Would it be possible to add functions to Twig within the theme PHP file (IE: antimatter.php)?
Community guidelines
Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.
Themes & Styling
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() }}.
@punchfighter in addition to @Utis comment read this (extend twig)
Log in to reply.
Suggested topics
| Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| 4 | 198 | 2 months ago | ||
| 3 | 94 | 2 months ago | ||
| 11 | 455 | 3 months ago | ||
| 0 | 48 | 3 months ago | ||
| 5 | 128 | 3 months ago |