diff --git a/CHANGELOG.md b/CHANGELOG.md index db5ab57ad..8082233a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index bbb6776bc..b1293404a 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -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: @@ -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 diff --git a/system/config/system.yaml b/system/config/system.yaml index ce0dd1ae5..d3d0580e8 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -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 diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 769459581..6528ad952 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -152,6 +152,23 @@ class Page 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 @@ -290,9 +307,13 @@ class Page $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)); @@ -313,6 +334,8 @@ class Page } $var = true; } + + } if ($var) {