Skip to content
Grav 2.0 is officially stable. Read the announcement →
Themes & Styling

Extending Twig in theme PHP file

Started by P. Fighter 8 years ago · 2 replies · 1076 views
8 years ago

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

8 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
8 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by stuart young, 2 weeks ago
3 251 2 weeks ago
Themes & Styling · by Sebadamus, 3 weeks ago
5 317 3 weeks ago
Themes & Styling · by martynfoster735, 4 weeks ago
1 310 4 weeks ago
Themes & Styling · by Justin Young, 1 month ago
1 348 1 month ago
Themes & Styling · by Slebeig, 2 months ago
4 474 2 months ago