diff --git a/doc/filters/default.rst b/doc/filters/default.rst index c4ccb56a5..e616e590c 100644 --- a/doc/filters/default.rst +++ b/doc/filters/default.rst @@ -21,6 +21,15 @@ undefined: .. code-block:: twig {{ var.method(foo|default('foo'))|default('foo') }} + +Using the ``default`` filter on a boolean variable might trigger unexpected behaviour, as +``false`` is treated as an empty value. Consider using ``??`` instead: + +.. code-block:: twig + + {% set foo = false %} + {{ foo|default(true) }} {# true #} + {{ foo ?? true }} {# false #} .. note:: diff --git a/src/Environment.php b/src/Environment.php index 86b517586..4888fad84 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -32,7 +32,7 @@ use Twig\RuntimeLoader\RuntimeLoaderInterface; use Twig\TokenParser\TokenParserInterface; /** - * Stores the Twig configuration. + * Stores the Twig configuration and renders templates. * * @author Fabien Potencier */ diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 8bc9c0d63..766d815a8 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -563,11 +563,11 @@ function twig_replace_filter($str, $from) */ function twig_round($value, $precision = 0, $method = 'common') { - if ('common' == $method) { + if ('common' === $method) { return round($value, $precision); } - if ('ceil' != $method && 'floor' != $method) { + if ('ceil' !== $method && 'floor' !== $method) { throw new RuntimeError('The round filter only supports the "common", "ceil", and "floor" methods.'); } @@ -1152,7 +1152,7 @@ function twig_to_array($seq, $preserveKeys = true) function twig_test_empty($value) { if ($value instanceof \Countable) { - return 0 == \count($value); + return 0 === \count($value); } if ($value instanceof \Traversable) {