Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

How to exclude page frontmatter from being cached?

Solved by Ron Wardenier View solution

Started by Ron Wardenier 8 years ago · 1 replies · 795 views
8 years ago

Setting cache_enable: false in a page header or frontmatter does seem to stop the content from being cached. The page is still present in the cache but it's content is always refreshed when the page is requested. That is my understanding which may be wrong of course.

In my case with dynamic Twig processed frontmatter I need the full page uncached.

In other words I am looking for the same behaviour as when caching is disabled system wide. But then of course only for some pages.

Because this need is related to a new plugin I'm creating I think I could recache a page as explained by Andy Miller in Grav issue #810.
The problem is though, I don't know and can't figure out how to use that function from a custom plugin.

Anyone to the rescue please?

8 years ago Solution

By adding the lines below to /system/src/Grav/Common/Page/Page.php#L620 the frontmatter is processed when system.pages.frontmatter.process_twig === true and page.header.cache_enable === false:

PHP
$frontmatter_process_twig = Grav::instance()['config']->get('system.pages.frontmatter.process_twig');

// process frontmatter if page is excluded from the cache 
if ($cache_enable === false && $frontmatter_process_twig === true) {
    $this->header = $content_obj['frontmatter'];
    $this->processFrontmatter();
}

This allows for dynamic frontmatter variables for example:

TWIG
now: '{{ ''now''|date(''Y-m-d H:i:s'') }}'

The value of now can then be used in markdown (if page.header.process.twig is true) with:

TWIG
The time is {{ header.now }}

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 43 1 week ago
Plugins · by Xavier, 4 weeks ago
2 54 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago