dynamic
Heads up! The dynamic tag only works if caching is enabled for the site. If caching is disabled, the dynamic tag doesn't do anything. Follow the checklist to enable caching.
The dynamic
block excludes everything inside from caching. The code inside the dynamic block is parsed on every request, even when caching is enabled. This way you can cache your pages, and at the same time keep parts of the code dynamic.
Input:
<!-- This timestamp is cached, so always returns the time when cache occurred -->
Cached time: {{ "now" | date: "%Y-%m-%d %H:%M" }}
{% dynamic %}
<!-- inside dynamic, so always returns the current time -->
Current time: {{ "now" | date: "%Y-%m-%d %H:%M" }}
{% enddynamic %}
Output:
<!-- This timestamp is cached, so always returns the time when cache occurred -->
Cached time: 2020-01-02 14:08
<!-- inside dynamic, so always returns the current time -->
Current time: 2020-01-06 14:08
It is not necessary to put both setting and getting of variables inside the dynamic
block. The following code will work:
{% assign blog_posts = site.blog_posts %}
{% dynamic %}
{% for post in blog_posts %} {{ post.title }} {% endfor %}
{% enddynamic %}