From 2e64d560b17070c8c4498dffdf9ce9dbde86aeae Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 10 Jan 2020 13:18:14 +0200 Subject: [PATCH] Fixed twig `{{ false|string }}` to return '0' instead of '' --- CHANGELOG.md | 1 + system/src/Grav/Common/Twig/TwigExtension.php | 37 ++++++++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aa060424..9a58b790b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index 4eb925b0f..6ac2b9133 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -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 *