mirror of
https://github.com/getgrav/grav.git
synced 2026-05-06 02:37:50 +02:00
Added yaml encode/decode filters
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ use Grav\Common\Markdown\ParsedownExtra;
|
||||
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
|
||||
{
|
||||
@@ -95,6 +96,8 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
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']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1167,4 +1170,26 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user