Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig (in Symfony) : access template parameters from twig extensions

I would like to access Twig template parameters from my twig extensions (filters, functions...) without passing it explicitly.

I always need a "displayPreferences" variable in all my twig extensions, in order to change the way to display and transform values.

It is possible to pass this variable as a template parameter, and pass it as an argument for each Twig filters / functions I run, but that makes the templates difficult to read.

Something like that would be great:

/**
 * Twig filter (render a date using the user defined format)
 *
 * @param Date $date
 */
public function renderUserDate ($date) {
    // Somehow, get a template parameter, without receiving it as argument
    $renderPreference = $this->accessTemplateParameter('displayPreferences');

    switch ($renderPreference['dateFormat']) {
        // Do something
    }
}
like image 289
Congelli501 Avatar asked Sep 17 '15 09:09

Congelli501


2 Answers

You can define a Context-aware Filters:

If you want to access the current context in your filter, set the needs_context option to true; Twig will pass the current context as the first argument to the filter call (or the second one if needs_environment is also set to true):

The context that is passed includes variables defined in the template.

So change the definition of the filter adding the required need_context parameters:

public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('price', array($this, 'renderUserDate', ,array('needs_context' => true)),
        );
    }

an then use as example:

/**
 * Twig filter (render a date using the user defined format)
 *
 * @param array $context: injected by twig
 * @param Date $date
 */
public function renderUserDate ($context, $date) {
    // defined in the template
    $renderPreference = $context['displayPreferences'];

    switch ($renderPreference['dateFormat']) {
        // Do something
    }
}
like image 93
Matteo Avatar answered Sep 28 '22 20:09

Matteo


In addition to being able to define context aware filters that let you reference template variables as noted in the accepted answer, you can also define context aware functions. The twig documentation on functions mentions this:

Functions support the same features as filters, except for the pre_escape and preserves_safety options.

Also, if you take a look at twig's code for functions it shows 'needs_context' as one of the available options.

Here is an example of a function that takes a passed value if supplied during the function call or uses the value from a context variable (template variable) if not:

public function getFunctions()
{
    return array(
        new \Twig_SimpleFunction('photobox', function($context, $page = false) {
            $page = $page ? $page : $context['page'];
            return $this->app['helper.theme']->photobox($page);
        }, array('needs_context' => true))
    );
}

Another quick tip that helped me when working with the context: if you want to see what variables are present in the context for you to reference in your twig function or filter simply reference twig's dump function {{ dump() }} in you template.

like image 30
PeterA Avatar answered Sep 28 '22 18:09

PeterA