[ticket/16885] Add type casting filters to Twig

PHPBB3-16885
This commit is contained in:
3D-I 2021-10-06 19:56:20 +02:00
parent 59cece1a0a
commit ea36b83ee9

View file

@ -79,6 +79,8 @@ class extension extends \Twig\Extension\AbstractExtension
new \Twig\TwigFilter('subset', array($this, 'loop_subset'), array('needs_environment' => true)),
// @deprecated 3.2.0 Uses twig's JS escape method instead of addslashes
new \Twig\TwigFilter('addslashes', 'addslashes'),
new \Twig\TwigFilter('int', [$this, 'type_int']),
new \Twig\TwigFilter('float', [$this, 'type_float']),
);
}
@ -165,6 +167,28 @@ class extension extends \Twig\Extension\AbstractExtension
return twig_slice($env, $item, $start, $end, $preserveKeys);
}
/**
* Cast to INT a variable
*
* Example: config('my_awesome_config')|int
* @return int
*/
public function type_int($value)
{
return (int) $value;
}
/**
* Cast to FLOAT a variable
*
* Example: config('my_awesome_config')|float
* @return float
*/
public function type_float($value)
{
return (float) $value;
}
/**
* Get output for a language variable (L_FOO, LA_FOO)
*