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.

Share:
21,160
Alex
Author by

Alex

Updated on July 05, 2022

Comments

  • Alex
    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
    Alex almost 13 years
    Think I will need to make my own function so I can use static ones. somthing like static('class', 'function', [params])
  • hakre
    hakre almost 13 years
    Probably not, try to register 'class::function' as the 'someFunction' parameter above and let me know if it works or not.
  • Alex
    Alex almost 13 years
    That's my problem, that might work, but then i would need to do it for every function i might need to use.
  • hakre
    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
    Alexander Morland over 12 years
    I'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
    slob almost 9 years
    How to make this first solution work when you pass full class name with its namespace?
  • Alfonso Fernandez-Ocampo
    Alfonso Fernandez-Ocampo almost 9 years
    Good one. I suppose this is not meant to be in a symphony 2 project
  • Alex
    Alex almost 9 years
    I was working with Yii at the time but this should be easy to work into Symphony.
  • Will B.
    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.
    Will B. about 8 years
    For 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.h‌​tml
  • Will B.
    Will B. about 8 years
    In php 5.6+ you can replace call_user_func_array with return $class::$method(...$arguments);