so where can I add this to include a twig function ?
In index.php I can include the functions file. But I need to include this twig_helpers.php after twig has loaded and I have to get twig object so I can add function to it. But it define in viewrenderer.php and use config. How can I reach it and add my custom function with a file.
So in index.php before ->run twig has loaded or not ? Because in my twig_helpers.php there is a lot of functions like this .
How can I define or get twig object so I can add functions to twig on yii2.
//How can I get twig or how can I add functions to twig in yii2 ??
$function = new Twig_SimpleFunction('some_function', 'some_function');
$twig->addFunction($function);
Hi I want to call all of my functions from one file. I do not know how to do this and the extension document is very poor I don’t understand how to use my functions and where to call them …
I am gonna create a class which extends \Twig_Extension like you did … Is it true ??
Because I do not want to add a code in your file with a composer update it will be gone away.
Just one question, I have created a class which extends \Twig_Extension and save it file to anywhere in my project. Than how can I load it or call it or where ?? I am gonna search the proper way for it, I do not want to choose a way on my own …
Sorry I am really a newbie for yii, I have never used it before so I am trying to understand…
I have created a file called TwigHelpers.php and save it in components folder
namespace yii\twig;
class TwigHelpers extends \Twig_Extension
{
public function getFunctions()
{
$options = [
'is_safe' => ['html'],
];
$functions = [
new \Twig_SimpleFunction('test', [$this, 'testprice'], $options),
];
return $functions;
}
public function testprice($var)
{
return $var;
}
public function getName()
{
return 'yii2-twig';
}
}
But when I call from twig file {{test(1)}} I get this error