2

How can I add a custom twig function to Craft CMS version 3?

Twig outlines how to add a function but not sure how to apply to Craft.

https://twig.symfony.com/doc/2.x/advanced.html#functions

$twig = new Twig_Environment($loader);
$function = new Twig_Function('function_name', function () {
    // ...
});
$twig->addFunction($function);
andreas
  • 16,357
  • 12
  • 72
  • 76
Tim Arney
  • 1,776
  • 2
  • 18
  • 23
  • 2
    https://craftcms.stackexchange.com/questions/9211/how-do-i-create-a-custom-twig-extension-which-includes-third-party-code?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – DarkBee Mar 23 '18 at 06:37
  • Do I need to create a plugin to add a twig function? – Tim Arney Mar 23 '18 at 11:23
  • If you are able to pinpoint the `twig` instance in `craftcms` you could go with adding one function. But i'm thinkng you are better off with going with the longer way. It will be easier in the longrun when u need to add more than one filter/function – DarkBee Mar 23 '18 at 12:12

0 Answers0