2

I'm trying to get my head around the following issue. I have a site which uses a base layout template base.twig, on which 3 layout templates are build (using extends). One of them is main.twig.

I want to use the paginate information variable pageInfo (see docs) in a block that's defined in the base template, to show the total number of entries found in the {% block pageTitle %}{% endblock %}. I tried a number of things, including defining a pageTitle variable in the base.twig template, to override in the content block, but all without any luck.

These are stripped down versions of my files:

_layouts/base.twig

<html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        {% block header %}{% endblock %}
        <main>
            {% block pageTitle %}{% endblock %}
            {% block main %}{% endblock %}
        </main>
        {% block footer %}{% endblock %}
    </body>
</html>

_layouts/main.twig

{% extends "_layouts/base" %}

{% block main %}
    <div class="main">
        {% block content %}{% endblock %}
    </div>
{% endblock %}

posts/index.twig

{% extends "_layouts/main" %}
{% set title = 'My posts listing' %}

{% set entries = craft.entries... %}

{% block content %}
    {% paginate entries as pageInfo, pageEntries %}
        <section>
            {% for entry in pageEntries %}
                <article>
                    <h2>{{ entry.title }}
                </article>
            {% endfor %}
        </section>

        {% block pageTitle %}
            <h1>{{ pageInfo.total }} entries found</h1>
        {% endblock %}

    {% endpaginate %}
{% endblock %}

I know this has to do with variable scopes, but how can I use the paginate information outside my content block? One solution I tried does indeed works, but do I really need to copy the {% paginate %} statement inside my pageTitle block to get this working? That seems to be very inefficient to me:

posts/index.twig

{% extends "_layouts/main" %}
{% set title = 'My posts listing' %}

{% set entries = craft.entries... %}

{% block content %}
    {% paginate entries as pageInfo, pageEntries %}
        <section>
            {% for entry in pageEntries %}
                <article>
                    <h2>{{ entry.title }}</h2>
                </article>
            {% endfor %}
        </section>
    {% endpaginate %}
{% endblock %}

{% block pageTitle %}
    {% paginate entries as pageInfo, pageEntries %}
        <h1>{{ pageInfo.total }} entries found</h1>
    {% endpaginate %}
{% endblock %}

Hope you can help me out or point me in the right direction. Any tips regarding a better structure for the blocks are also welcome. Twig seems so popular, so you'd think it can't be that hard for such simple tasks? Maybe I'm using the whole block concept totally wrong in the first place.

3

As of Craft 2.5.2750, you can leave off the {% endpaginate %} tag, then move your {% paginate %} tag up outside of the blocks in the posts/index.twig template, and your pageInfo variable should be available to the whole template, including the parent template that you're extending.

  • Thanks Mike! Clean and very simple, I took some code from templates pre december 2015. I could've saved me some time if I've read the docs before posting it here. It works like a charm now. Thanks again. – ctigelaar Feb 2 '16 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.