Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Extending Twig in theme PHP file

Started by P. Fighter 9 years ago · 2 replies · 1165 views
9 years ago

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

9 years ago

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

PHP
// ...
   public function onTwigExtensions()
   {
       $function = new \Twig_SimpleFunction ('mytest', function () {
           return "This is a test.";
       });
       $this->grav['twig']->twig->addFunction($function);                   
   }

// ...

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

👍 1
9 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by FrViPofm, 4 weeks ago
4 472 2 weeks ago
Themes & Styling · by johan thomas, 5 years ago
1 799 3 weeks ago
Themes & Styling · by stuart young, 2 months ago
3 657 2 months ago
Themes & Styling · by Sebadamus, 3 months ago
5 678 2 months ago
Themes & Styling · by martynfoster735, 3 months ago
1 641 3 months ago