Skip to content

Commit

Permalink
Added ability to never cache twig. This makes it possible to cache co…
Browse files Browse the repository at this point in the history
…ntent, but always process twig. Useful for regular but especially modular pages. (#1244)
  • Loading branch information
rhukster authored Jan 12, 2017
1 parent 9ebff22 commit 759ba51
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
11 changes: 11 additions & 0 deletions system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions system/blueprints/pages/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 43 additions & 17 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,38 +572,64 @@ 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();
}

// 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();
}
}
}

Expand Down

0 comments on commit 759ba51

Please sign in to comment.