Fixed twig {{ false|string }} to return '0' instead of ''

This commit is contained in:
Matias Griese
2020-01-10 13:18:14 +02:00
parent 82270e0c13
commit 2e64d560b1
2 changed files with 17 additions and 21 deletions

View File

@@ -10,6 +10,7 @@
* Fixed PHP 7.4 compatibility issue with `Stream`
* Fixed new `Flex Users` being stored with wrong filename, login issues [#2785](https://github.com/getgrav/grav/issues/2785)
* Fixed `ignore_empty: true` not removing empty values in blueprint filtering
* Fixed twig `{{ false|string }}` to return '0' instead of ''
# v1.7.0-rc.3
## 01/02/2020

View File

@@ -167,7 +167,7 @@ class TwigExtension extends AbstractExtension implements GlobalsInterface
new TwigFunction('repeat', [$this, 'repeatFunc']),
new TwigFunction('regex_replace', [$this, 'regexReplace']),
new TwigFunction('regex_filter', [$this, 'regexFilter']),
new TwigFunction('string', [$this, 'stringFunc']),
new TwigFunction('string', [$this, 'stringFilter']),
new TwigFunction('url', [$this, 'urlFunc']),
new TwigFunction('json_decode', [$this, 'jsonDecodeFilter']),
new TwigFunction('get_cookie', [$this, 'getCookie']),
@@ -689,14 +689,25 @@ class TwigExtension extends AbstractExtension implements GlobalsInterface
}
/**
* Casts input to string.
* Returns a string from a value. If the value is array, return it json encoded
*
* @param mixed $input
* @param mixed $value
* @return string
*/
public function stringFilter($input)
public function stringFilter($value)
{
return (string) $input;
// Format the array as a string
if (is_array($value)) {
return json_encode($value);
}
// Boolean becomes '1' or '0'
if (is_bool($value)) {
$value = (int)$value;
}
// Cast the other values to string.
return (string)$value;
}
/**
@@ -992,22 +1003,6 @@ class TwigExtension extends AbstractExtension implements GlobalsInterface
return array_intersect($array1, $array2);
}
/**
* Returns a string from a value. If the value is array, return it json encoded
*
* @param array|string $value
*
* @return string
*/
public function stringFunc($value)
{
if (is_array($value)) { //format the array as a string
return json_encode($value);
}
return $value;
}
/**
* Translate a string
*