twig template engine, using a static function or variable
21,160
Solution 1
Couple ways I've ended up doing it.
First is a function that can call a static function.
$twig = new Twig_Environment($loader);
$twig->addFunction('staticCall', new Twig_Function_Function('staticCall'));
function staticCall($class, $function, $args = array())
{
if (class_exists($class) && method_exists($class, $function))
return call_user_func_array(array($class, $function), $args);
return null;
}
Can then be used like,
{{ staticCall('myClass', 'mymethod', [optional params]) }}
The other is to use a magic method.
Add the class to the render $context
$data['TwigRef'] = new TheClass();
class TheClass
{
public function __call($name, $arguments) {
return call_user_func_array(array('TheClass', $name), $arguments);
}
...
}
Can then be used like,
{{ TwigRef.myMethod(optional params) }}
Probably best to add some extra checks so only approved functions call be called.
Solution 2
You can dynamically add functions to your twig templates by registering them. Either they are already callable or you alias your static function by a name of it's own:
$twig = new Twig_Environment($loader);
$twig->addFunction('functionName', new Twig_Function_Function('someFunction'));
See the Functions section in Extending Twig.
Author by
Alex
Updated on July 05, 2022Comments
-
Alex almost 2 years
Is there a way to call a static function or use a static variable in twig?
I have a class of static helper functions and want to use one or two in a template.
-
Alex almost 13 yearsThink I will need to make my own function so I can use static ones. somthing like static('class', 'function', [params])
-
hakre almost 13 yearsProbably not, try to register
'class::function'
as the'someFunction'
parameter above and let me know if it works or not. -
Alex almost 13 yearsThat's my problem, that might work, but then i would need to do it for every function i might need to use.
-
hakre almost 13 years@Alex: Create another helper function that returns all functions to register in form of an array (key: name, value: callback), then just iterate over the array and automatically assign them. The helper class should know which helpers it offers, so you keep everything together.
-
Alexander Morland over 12 yearsI've seen this before. I have yet to see a placement for it. Please, in a SF2 project, where is this code suppose to go? Where is $loader defined?
-
slob almost 9 yearsHow to make this first solution work when you pass full class name with its namespace?
-
Alfonso Fernandez-Ocampo almost 9 yearsGood one. I suppose this is not meant to be in a symphony 2 project
-
Alex almost 9 yearsI was working with Yii at the time but this should be easy to work into Symphony.
-
Will B. about 8 years@AlexanderMorland see twig.sensiolabs.org/doc/api.html effectively
$loader
should be defined in your application config that instantiates twig. -
Will B. about 8 yearsFor implementing this in Symfony see the Extending Twig section of the cookbook for your version of Symfony. symfony.com/doc/current/cookbook/templating/twig_extension.html
-
Will B. about 8 yearsIn php 5.6+ you can replace
call_user_func_array
withreturn $class::$method(...$arguments);