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.