From d932db420a6e547af18617afd6d6e9bab4e25f33 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 11 Jan 2017 21:34:51 -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. --- system/blueprints/config/system.yaml | 11 +++++ system/blueprints/pages/default.yaml | 24 +++++++++++ system/config/system.yaml | 1 + system/src/Grav/Common/Page/Page.php | 60 ++++++++++++++++++++-------- 4 files changed, 79 insertions(+), 17 deletions(-) diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index a55c1e1ded..3e089d83d0 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 5fb9ab232b..4fd60722d6 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 54e4c47816..7f7ba4baf5 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 95da4050cd..013a3ab324 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -572,16 +572,16 @@ public function content($var = null) $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 @@ public function content($var = null) // Content Processed but not cached yet Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this])); - } else { - if ($process_markdown) { - $this->processMarkdown(); + if ($cache_enable) { + $this->cachePageContent(); } + } - // Content Processed but not cached yet - Grav::instance()->fireEvent('onPageContentProcessed', new Event(['page' => $this])); + 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 ($process_twig) { - $this->processTwig(); + 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(); + if ($cache_enable) { + $this->cachePageContent(); + } } }