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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Extending Twig in theme PHP file

Started by P. Fighter 8 years ago · 2 replies · 981 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 Pedro M, 2 months ago
4 196 2 months ago
Themes & Styling · by Ian, 2 months ago
3 92 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 454 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 47 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 126 3 months ago