From 759ba5143f23e4e755a72b80bb934c54fccae8aa Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 12 Jan 2017 11:51:12 -0700 Subject: [PATCH] Added ability to never cache twig. This makes it possible to cache content, but always process twig. Useful for regular but especially modular pages. (#1244) --- system/blueprints/config/system.yaml | 11 +++++ system/blueprints/pages/default.yaml | 24 +++++++++++ system/config/system.yaml | 1 + system/src/Grav/Common/Page/Page.php | 64 +++++++++++++++++++--------- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index 56066e3f9..d8b0a24a6 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -244,6 +244,17 @@ form: validate: type: bool + pages.never_cache_twig: + type: toggle + label: PLUGIN_ADMIN.NEVER_CACHE_TWIG + help: PLUGIN_ADMIN.NEVER_CACHE_TWIG_HELP + highlight: 0 + options: + 1: PLUGIN_ADMIN.YES + 0: PLUGIN_ADMIN.NO + validate: + type: bool + pages.frontmatter.process_twig: type: toggle label: PLUGIN_ADMIN.FRONTMATTER_PROCESS_TWIG diff --git a/system/blueprints/pages/default.yaml b/system/blueprints/pages/default.yaml index 5fb9ab232..4fd60722d 100644 --- a/system/blueprints/pages/default.yaml +++ b/system/blueprints/pages/default.yaml @@ -225,6 +225,30 @@ form: twig: Twig use: keys + header.twig_first: + type: toggle + toggleable: true + label: PLUGIN_ADMIN.TWIG_FIRST + help: PLUGIN_ADMIN.TWIG_FIRST_HELP + highlight: 0 + options: + 1: PLUGIN_ADMIN.YES + 0: PLUGIN_ADMIN.NO + validate: + type: bool + + header.never_cache_twig: + type: toggle + toggleable: true + label: PLUGIN_ADMIN.NEVER_CACHE_TWIG + help: PLUGIN_ADMIN.NEVER_CACHE_TWIG_HELP + highlight: 0 + options: + 1: PLUGIN_ADMIN.YES + 0: PLUGIN_ADMIN.NO + validate: + type: bool + header.child_type: type: select toggleable: true diff --git a/system/config/system.yaml b/system/config/system.yaml index 54e4c4781..7f7ba4baf 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -36,6 +36,7 @@ pages: markdown: true # Process Markdown twig: false # Process Twig twig_first: false # Process Twig before markdown when processing both on a page + never_cache_twig: false # Only cache content, never cache twig processed in content (incompatible with `twig_first: true`) events: page: true # Enable page level events twig: true # Enable Twig level events diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 95da4050c..013a3ab32 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -572,16 +572,16 @@ class Page $twig_first = isset($this->header->twig_first) ? $this->header->twig_first : $config->get('system.pages.twig_first', true); + // never cache twig means it's always run after content + $never_cache_twig = isset($this->header->never_cache_twig) ? $this->header->never_cache_twig : $config->get('system.pages.never_cache_twig', + false); // if no cached-content run everything - if ($this->content === false || $cache_enable === false) { - $this->content = $this->raw_content; - Grav::instance()->fireEvent('onPageContentRaw', new Event(['page' => $this])); + if ($never_cache_twig) { + if ($this->content === false || $cache_enable === false) { + $this->content = $this->raw_content; + Grav::instance()->fireEvent('onPageContentRaw', new Event(['page' => $this])); - if ($twig_first) { - if ($process_twig) { - $this->processTwig(); - } if ($process_markdown) { $this->processMarkdown(); } @@ -589,21 +589,47 @@ class Page // Content Processed but not cached yet Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this])); - } else { - if ($process_markdown) { - $this->processMarkdown(); - } - - // Content Processed but not cached yet - Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this])); - - if ($process_twig) { - $this->processTwig(); + if ($cache_enable) { + $this->cachePageContent(); } } - if ($cache_enable) { - $this->cachePageContent(); + if ($process_twig) { + $this->processTwig(); + } + + } else { + if ($this->content === false || $cache_enable === false) { + $this->content = $this->raw_content; + Grav::instance()->fireEvent('onPageContentRaw', new Event(['page' => $this])); + + if ($twig_first) { + if ($process_twig) { + $this->processTwig(); + } + if ($process_markdown) { + $this->processMarkdown(); + } + + // Content Processed but not cached yet + Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this])); + + } else { + if ($process_markdown) { + $this->processMarkdown(); + } + + // Content Processed but not cached yet + Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this])); + + if ($process_twig) { + $this->processTwig(); + } + } + + if ($cache_enable) { + $this->cachePageContent(); + } } }