mirror of
https://github.com/getgrav/grav.git
synced 2025-12-05 15:29:57 +01:00
Added twig filters for casting values: |string, |int, |bool, |float, |array
Made `|markdown` filter HTML safe
This commit is contained in:
@@ -1,3 +1,11 @@
|
||||
# v1.5.0-beta.3
|
||||
## mm/dd/2018
|
||||
|
||||
1. [](#new)
|
||||
* Added twig filters for casting values: `|string`, `|int`, `|bool`, `|float`, `|array`
|
||||
1. [](#bugfix)
|
||||
* Made `|markdown` filter HTML safe
|
||||
|
||||
# v1.5.0-beta.2
|
||||
## 07/13/2018
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
new \Twig_SimpleFilter('fieldName', [$this, 'fieldNameFilter']),
|
||||
new \Twig_SimpleFilter('ksort', [$this, 'ksortFilter']),
|
||||
new \Twig_SimpleFilter('ltrim', [$this, 'ltrimFilter']),
|
||||
new \Twig_SimpleFilter('markdown', [$this, 'markdownFunction']),
|
||||
new \Twig_SimpleFilter('markdown', [$this, 'markdownFunction'], ['is_safe' => ['html']]),
|
||||
new \Twig_SimpleFilter('md5', [$this, 'md5Filter']),
|
||||
new \Twig_SimpleFilter('base32_encode', [$this, 'base32EncodeFilter']),
|
||||
new \Twig_SimpleFilter('base32_decode', [$this, 'base32DecodeFilter']),
|
||||
@@ -88,9 +88,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
new \Twig_SimpleFilter('safe_truncate_html', ['\Grav\Common\Utils', 'safeTruncateHTML']),
|
||||
new \Twig_SimpleFilter('sort_by_key', [$this, 'sortByKeyFilter']),
|
||||
new \Twig_SimpleFilter('starts_with', [$this, 'startsWithFilter']),
|
||||
new \Twig_SimpleFilter('t', [$this, 'translate']),
|
||||
new \Twig_SimpleFilter('tl', [$this, 'translateLanguage']),
|
||||
new \Twig_SimpleFilter('ta', [$this, 'translateArray']),
|
||||
new \Twig_SimpleFilter('truncate', ['\Grav\Common\Utils', 'truncate']),
|
||||
new \Twig_SimpleFilter('truncate_html', ['\Grav\Common\Utils', 'truncateHTML']),
|
||||
new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecodeFilter']),
|
||||
@@ -100,6 +97,18 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
new \Twig_SimpleFilter('print_r', 'print_r'),
|
||||
new \Twig_SimpleFilter('yaml_encode', [$this, 'yamlEncodeFilter']),
|
||||
new \Twig_SimpleFilter('yaml_decode', [$this, 'yamlDecodeFilter']),
|
||||
|
||||
// Translations
|
||||
new \Twig_SimpleFilter('t', [$this, 'translate']),
|
||||
new \Twig_SimpleFilter('tl', [$this, 'translateLanguage']),
|
||||
new \Twig_SimpleFilter('ta', [$this, 'translateArray']),
|
||||
|
||||
// Casting values
|
||||
new \Twig_SimpleFilter('string', [$this, 'stringFilter']),
|
||||
new \Twig_SimpleFilter('int', [$this, 'intFilter'], ['is_safe' => true]),
|
||||
new \Twig_SimpleFilter('bool', [$this, 'boolFilter']),
|
||||
new \Twig_SimpleFilter('float', [$this, 'floatFilter'], ['is_safe' => true]),
|
||||
new \Twig_SimpleFilter('array', [$this, 'arrayFilter']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -111,7 +120,7 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new \Twig_SimpleFunction('array', [$this, 'arrayFunc']),
|
||||
new \Twig_SimpleFunction('array', [$this, 'arrayFilter']),
|
||||
new \Twig_SimpleFunction('array_key_value', [$this, 'arrayKeyValueFunc']),
|
||||
new \Twig_SimpleFunction('array_key_exists', 'array_key_exists'),
|
||||
new \Twig_SimpleFunction('array_unique', 'array_unique'),
|
||||
@@ -132,9 +141,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
new \Twig_SimpleFunction('regex_replace', [$this, 'regexReplace']),
|
||||
new \Twig_SimpleFunction('regex_filter', [$this, 'regexFilter']),
|
||||
new \Twig_SimpleFunction('string', [$this, 'stringFunc']),
|
||||
new \Twig_simpleFunction('t', [$this, 'translate']),
|
||||
new \Twig_simpleFunction('tl', [$this, 'translateLanguage']),
|
||||
new \Twig_simpleFunction('ta', [$this, 'translateArray']),
|
||||
new \Twig_SimpleFunction('url', [$this, 'urlFunc']),
|
||||
new \Twig_SimpleFunction('json_decode', [$this, 'jsonDecodeFilter']),
|
||||
new \Twig_SimpleFunction('get_cookie', [$this, 'getCookie']),
|
||||
@@ -151,6 +157,10 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
new \Twig_SimpleFunction('nicefilesize', [$this, 'niceFilesizeFunc']),
|
||||
new \Twig_SimpleFunction('nicetime', [$this, 'nicetimeFilter']),
|
||||
|
||||
// Translations
|
||||
new \Twig_simpleFunction('t', [$this, 'translate']),
|
||||
new \Twig_simpleFunction('tl', [$this, 'translateLanguage']),
|
||||
new \Twig_simpleFunction('ta', [$this, 'translateArray']),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -617,6 +627,62 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
return ltrim($value, $chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts input to string.
|
||||
*
|
||||
* @param mixed $input
|
||||
* @return string
|
||||
*/
|
||||
public function stringFilter($input)
|
||||
{
|
||||
return (string) $input;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Casts input to int.
|
||||
*
|
||||
* @param mixed $input
|
||||
* @return int
|
||||
*/
|
||||
public function intFilter($input)
|
||||
{
|
||||
return (int) $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts input to bool.
|
||||
*
|
||||
* @param mixed $input
|
||||
* @return bool
|
||||
*/
|
||||
public function boolFilter($input)
|
||||
{
|
||||
return (bool) $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts input to float.
|
||||
*
|
||||
* @param mixed $input
|
||||
* @return float
|
||||
*/
|
||||
public function floatFilter($input)
|
||||
{
|
||||
return (float) $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts input to array.
|
||||
*
|
||||
* @param mixed $input
|
||||
* @return array
|
||||
*/
|
||||
public function arrayFilter($input)
|
||||
{
|
||||
return (array) $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
@@ -693,7 +759,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
|
||||
$template = $env->createTemplate($twig);
|
||||
return $template->render($context);
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -788,19 +853,6 @@ class TwigExtension extends \Twig_Extension implements \Twig_Extension_GlobalsIn
|
||||
return str_pad($input, (int)$pad_length, $pad_string, $pad_type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cast a value to array
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function arrayFunc($value)
|
||||
{
|
||||
return (array)$value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Workaround for twig associative array initialization
|
||||
* Returns a key => val array
|
||||
|
||||
Reference in New Issue
Block a user