Extending Twig in theme PHP file

Would it be possible to add functions to Twig within the theme PHP file (IE: antimatter.php)?

Yes! In the class named after the theme inside a method onTwigExtensions, you can add something like:

// ...
   public function onTwigExtensions()
       $function = new \Twig_SimpleFunction ('mytest', function () {
           return "This is a test.";

// ...

And use it in Twig with {{ mytest() }}.

1 Like

@punchfighter in addition to @Utis comment read this (extend twig)