What's on this page
Speed up builds with fragments cache
Some parts of a layout (footer, sidebar, list of latest posts, etc.) are identical on every page but are rendered again for each of them. With the fragments cache, Cecil renders such a part once per build and language, then reuses the result for the other pages.
Wrap content with the cache tag
Surround the cacheable part of a template with the cache tag and a key built with the cache_key function:
{% cache cache_key('footer') %}
<footer>
{% for entry in site.menus.footer|sort_by_weight %}
<a href="{{ url(entry.url) }}">{{ entry.name }}</a>
{% endfor %}
</footer>
{% endcache %}
Build the cache key
The cache_key function builds a key from a name, an optional value, the current language and the build ID:
{% cache cache_key('sidebar', page.section) %}
{# cacheable content #}
{% endcache %}
- the language ensures that each language gets its own fragment
- the build ID changes on every build, so a fragment is rendered once per build and never reused by a later build
- the optional value (string, array or object) is hashed and added to the key, to keep several variants of the same fragment, for example one per section
Example: latest posts sidebar
The list below is rendered once per build and language, then reused by every page that includes it:
{% cache cache_key('latest-posts') %}
<aside>
<h2>Latest posts</h2>
<ul>
{% for post in site.pages.showable|filter_by('section', 'blog')|sort_by_date|slice(0, 5) %}
<li><a href="{{ url(post) }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
</aside>
{% endcache %}
Clear the cache
Clear only the fragments cache:
php cecil.phar cache:clear:templates --fragments
Or clear the whole templates cache, or all caches:
php cecil.phar cache:clear:templates
php cecil.phar cache:clear
During local development, you can also clear the cache before each build:
php cecil.phar serve --clear-cache