Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Extending Twig in theme PHP file

Started by P. Fighter 8 years ago · 2 replies · 1116 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 FrViPofm, 14 hours ago
1 25 53 minutes ago
Themes & Styling · by stuart young, 1 month ago
3 480 1 month ago
Themes & Styling · by Sebadamus, 2 months ago
5 481 2 months ago
Themes & Styling · by martynfoster735, 2 months ago
1 460 2 months ago
Themes & Styling · by Justin Young, 2 months ago
1 490 2 months ago