mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-27 09:49:22 +02:00
Add TwigTypeCastingExtension
Useful for casting variables in Twig
This commit is contained in:
parent
8b0b123f42
commit
7181b988e9
36
system/libs/TwigTypeCastingExtension.php
Normal file
36
system/libs/TwigTypeCastingExtension.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MyAAC\Twig\Extension;
|
||||||
|
|
||||||
|
use Twig\Extension\AbstractExtension;
|
||||||
|
use Twig\TwigFilter;
|
||||||
|
|
||||||
|
final class TwigTypeCastingExtension extends AbstractExtension
|
||||||
|
{
|
||||||
|
/** @return array<int, TwigFilter> */
|
||||||
|
public function getFilters(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new TwigFilter('int', function ($value) {
|
||||||
|
return (int)$value;
|
||||||
|
}),
|
||||||
|
new TwigFilter('float', function ($value) {
|
||||||
|
return (float)$value;
|
||||||
|
}),
|
||||||
|
new TwigFilter('string', function ($value) {
|
||||||
|
return (string)$value;
|
||||||
|
}),
|
||||||
|
new TwigFilter('bool', function ($value) {
|
||||||
|
return (bool)$value;
|
||||||
|
}),
|
||||||
|
new TwigFilter('array', function (object $value) {
|
||||||
|
return (array)$value;
|
||||||
|
}),
|
||||||
|
new TwigFilter('object', function (array $value) {
|
||||||
|
return (object)$value;
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,9 @@ if($dev_mode) {
|
|||||||
}
|
}
|
||||||
unset($dev_mode);
|
unset($dev_mode);
|
||||||
|
|
||||||
|
require LIBS . 'TwigTypeCastingExtension.php';
|
||||||
|
$twig->addExtension(new MyAAC\Twig\Extension\TwigTypeCastingExtension());
|
||||||
|
|
||||||
$function = new TwigFunction('getStyle', function ($i) {
|
$function = new TwigFunction('getStyle', function ($i) {
|
||||||
return getStyle($i);
|
return getStyle($i);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user