From 1186f94e21a8ea85833db1ba8cf3b6dba0582f7a Mon Sep 17 00:00:00 2001 From: slawkens Date: Mon, 8 Apr 2024 10:08:48 +0200 Subject: [PATCH] Add Twig TypeCastingExtension --- .../Twig/Extension/TypeCastingExtension.php | 36 +++++++++++++++++++ system/twig.php | 2 ++ 2 files changed, 38 insertions(+) create mode 100644 system/src/Twig/Extension/TypeCastingExtension.php diff --git a/system/src/Twig/Extension/TypeCastingExtension.php b/system/src/Twig/Extension/TypeCastingExtension.php new file mode 100644 index 0000000000..cb15c0ad68 --- /dev/null +++ b/system/src/Twig/Extension/TypeCastingExtension.php @@ -0,0 +1,36 @@ + */ + 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; + }), + ]; + } +} diff --git a/system/twig.php b/system/twig.php index b07a3a4c7b..9176c37116 100644 --- a/system/twig.php +++ b/system/twig.php @@ -31,6 +31,8 @@ } unset($dev_mode); +$twig->addExtension(new MyAAC\Twig\Extension\TypeCastingExtension()); + $filter = new TwigFilter('timeago', function ($datetime) { $time = time() - strtotime($datetime);