Skip to content

Commit

Permalink
Feature/frontmatter twig processing (#788)
Browse files Browse the repository at this point in the history
* start on processFrontmatter logic

* optimized the twig processing

* Added blueprint definitions

* updated changelog

* Fix to not process in admin
  • Loading branch information
rhukster committed Apr 14, 2016
1 parent 5213867 commit 6124f71
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* **Custom Form Field Types**: This advanced new functionality allows you to create a custom field type via a new plugin event called getFormFieldTypes(). This allows you to provide extra functionality or instructions on how to handle the form form field.
* **GPM Versioning**: A new feature that we have wanted to add to our GPM package management system is the ability to control dependencies by version. We have opted to use a syntax very similar to the Composer Package Manager that is already familiar to most PHP developers. This new versioning system allows you to define specific minimum version requirements of dependent packages within Grav. This should ensure that we have less (hopefully none!) issues when you update one package that also requires a specific minimum version of another package. The admin plugin for example may have an update that requires a specific version of Grav itself.
* Refactor of the process chain breaking out `Processors` into individual classes to allow for easier modification and addition. Thanks to toovy for this work. - [#745](https://github.com/getgrav/grav/pull/745)
* Added optional config to allow Twig processing in page frontmatter - [#788](https://github.com/getgrav/grav/pull/788)
* Added the ability to provide blueprints via a plugin (previously limited to Themes only).
* Added Developer CLI Tools to easily create a new theme or plugin
* Allow authentication for proxies - [#698](https://github.com/getgrav/grav/pull/698)
Expand Down
23 changes: 21 additions & 2 deletions system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,6 @@ form:
pages.twig_first:
type: toggle
label: PLUGIN_ADMIN.TWIG_FIRST
highlight: asc
default: desc
help: PLUGIN_ADMIN.TWIG_FIRST_HELP
highlight: 0
options:
Expand All @@ -244,6 +242,27 @@ form:
validate:
type: bool

pages.frontmatter.process_twig:
type: toggle
label: PLUGIN_ADMIN.FRONTMATTER_PROCESS_TWIG
help: PLUGIN_ADMIN.FRONTMATTER_PROCESS_TWIG_HELP
highlight: 0
options:
1: PLUGIN_ADMIN.YES
0: PLUGIN_ADMIN.NO
validate:
type: bool

pages.frontmatter.ignore_fields:
type: selectize
size: large
placeholder: "e.g. forms"
label: PLUGIN_ADMIN.FRONTMATTER_IGNORE_FIELDS
help: PLUGIN_ADMIN.FRONTMATTER_IGNORE_FIELDS_HELP
classes: fancy
validate:
type: commalist

languages:
type: section
title: PLUGIN_ADMIN.LANGUAGES
Expand Down
3 changes: 3 additions & 0 deletions system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pages:
ignore_folders: [.git, .idea] # Folders to ignore in Pages
ignore_hidden: true # Ignore all Hidden files and folders
url_taxonomy_filters: true # Enable auto-magic URL-based taxonomy filters for page collections
frontmatter:
process_twig: false # Should the frontmatter be processed to replace Twig variables?
ignore_fields: ['form'] # Fields that might contain Twig variables and should not be processed

cache:
enabled: true # Set to true to enable caching
Expand Down
27 changes: 25 additions & 2 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ public function init(\SplFileInfo $file, $extension = null)
return $this;
}

protected function processFrontmatter()
{
// Quick check for twig output tags in frontmatter if enabled
if (Utils::contains($this->frontmatter, '{{')) {
$process_fields = $this->file()->header();
$ignored_fields = [];
foreach ((array)Grav::instance()['config']->get('system.pages.frontmatter.ignore_fields') as $field) {
if (isset($process_fields[$field])) {
$ignored_fields[$field] = $process_fields[$field];
unset($process_fields[$field]);
}
}
$text_header = Grav::instance()['twig']->processString(json_encode($process_fields), ['page'=>$this]);
$this->header((object) (json_decode($text_header, true) + $ignored_fields));
}
}

/**
* Return an array with the routes of other translated languages
* @return array the page translated languages
Expand Down Expand Up @@ -290,9 +307,13 @@ public function header($var = null)
$this->frontmatter = $file->frontmatter();
$this->header = (object)$file->header();

// If there's a `frontmatter.yaml` file merge that in with the page header
// note page's own frontmatter has precedence and will overwrite any defaults
if (!Utils::isAdminPlugin()) {
// Process frontmatter with Twig if enabled
if (Grav::instance()['config']->get('system.pages.frontmatter.process_twig') === true) {
$this->processFrontmatter();
}
// If there's a `frontmatter.yaml` file merge that in with the page header
// note page's own frontmatter has precedence and will overwrite any defaults
$frontmatter_file = $this->path . '/' . $this->folder . '/frontmatter.yaml';
if (file_exists($frontmatter_file)) {
$frontmatter_data = (array)Yaml::parse(file_get_contents($frontmatter_file));
Expand All @@ -313,6 +334,8 @@ public function header($var = null)
}
$var = true;
}


}

if ($var) {
Expand Down

0 comments on commit 6124f71

Please sign in to comment.