简体   繁体   English

Twig(在Symfony中):从twig扩展访问模板参数

[英]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. 我想从我的twig扩展(过滤器,函数...)访问Twig模板参数,而不显式传递它。

I always need a "displayPreferences" variable in all my twig extensions, in order to change the way to display and transform values. 我总是需要在所有树枝扩展中使用“displayPreferences”变量,以便更改显示和转换值的方式。

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. 可以将此变量作为模板参数传递,并将其作为我运行的每个Twig过滤器/函数的参数传递,但这会使模板难以阅读。

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
    }
}

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; 如果要访问过滤器中的当前上下文,请将needs_context选项设置为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): Twig将当前上下文作为过滤器调用的第一个参数传递(如果needs_environment也设置为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: 因此,更改过滤器的定义,添加所需的need_context参数:

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
    }
}

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: 关于函数twig文档提到了这一点:

Functions support the same features as filters, except for the pre_escape and preserves_safety options. 函数支持与过滤器相同的功能,但pre_escape和preserves_safety选项除外。

Also, if you take a look at twig's code for functions it shows 'needs_context' as one of the available options. 此外,如果您查看twig的函数代码,它会将'needs_context'显示为可用选项之一。

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. 在处理上下文时帮助我的另一个快速提示:如果要查看上下文中存在哪些变量供您在twig函数或过滤器中引用,只需在模板中引用twig的转储函数 {{ dump() }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM