Skip to content

Commit

Permalink
Added yaml encode/decode filters
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Jan 25, 2018
1 parent 47746d3 commit c721be8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
## mm/dd/2018

1. [](#new)
* Added new `Collection:toExtendedArray()` method that's particularly useful for Json output of data
* Added new `Collection::toExtendedArray()` method that's particularly useful for Json output of data
* Added new `|yaml_encode` and `|yaml_decode` Twig filter to convert to and from YAML
1. [](#improved)
* Better `Page.collection()` filtering support including ability to have non-published pages in collections

Expand Down
25 changes: 25 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Grav\Common\Uri;
use Grav\Common\Helpers\Base32;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use Symfony\Component\Yaml\Yaml;

class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
Expand Down Expand Up @@ -95,6 +96,8 @@ public function getFilters()
new \Twig_SimpleFilter('basename', 'basename'),
new \Twig_SimpleFilter('dirname', 'dirname'),
new \Twig_SimpleFilter('print_r', 'print_r'),
new \Twig_SimpleFilter('yaml_encode', [$this, 'yamlEncodeFilter']),
new \Twig_SimpleFilter('yaml_decode', [$this, 'yamlDecodeFilter']),
];
}

Expand Down Expand Up @@ -1167,4 +1170,26 @@ public function pageHeaderVarFunc($var, $pages = null)
}
}
}

/**
* Dump/Encode data into YAML format
*
* @param $data
* @return mixed
*/
public function yamlEncodeFilter($data)
{
return Yaml::dump($data, 10);
}

/**
* Decode/Parse data from YAML format
*
* @param $data
* @return mixed
*/
public function yamlDecodeFilter($data)
{
return Yaml::parse($data);
}
}

0 comments on commit c721be8

Please sign in to comment.