跳转到主要内容
RM 提交于 4 July 2015
原文链接:Discovering and Inspecting Variables in Twig Templates>

在一个Twig模板文件里,你可以发现注释里有很多可用的变量。然而,当变量不是记录在模板文件中,或者是当主题、模块引入新的变量时,我们就需要一种方法来检索和提取模板里所有可用变量的和特定范围的变量,Twig支持在模板文件里使用dump() 函数(转存函数)来提取和检索变量。

Dump函数在启用调试之前不会显示输出的变量内容。学习如何启用Twig调试

开启调试后,dump函数就可以输出模板里的一个变量或多个变量的相关信息。

检索单个变量

假设你的模板里有一个title变量,可以按如下形式,将其内容dump到你的模板:

{{ dump(title) }}

检索模板内所有可用变量

将模板内所有可用的变量和变量的内容添加到你的模板,可用如下示例代码:

{{ dump() }}

Dump(转存)一个只有一个可用变量keys使用如下代码:

{{ dump(_context|keys) }}

以下全局变量可用于所有的Twig模板:

  • _self引用了当前模板以及模板包含的高级信息,也就是已编译的模板类名和Twig的环境信息。
  • _context 引用当前的上下文和上下文所包含的所有变量,然后传递给模板,例如一个来自theme()的变量,由预处理得来,或来自模板。增加一个{{ dump() }} 却没有定义一个变量,相当于添加了一个{{ dump(_context) }}。
  • _charset 用于引用当前的字符集。

使用dump()需注意:

如果你想看到模板里所有的变量,但是因dump()(转存函数)的递归或其他类似递归的算法逻辑而导致内存不足,你可以遍历_context来引用当前上下文所包含的所有变量,从而看到所有的keys,示例如下:

<ol>
    {% for key, value in _context  %}
      <li>{{ key }}</li>
    {% endfor %}
</ol>

然后用一个条件判断来检查结果(例如 {{if loop.index=2%}}),然后dump(转存)有需要的变量即可。

更多信息

查看Twig的dump()函数文档了解更多关于dump()函数的知识。


附上英文版对照查看:

When working with a Twig template file most variables are documented in the comments for the template file. However, when they are not, or when themes or modules introduce new variables we need a way to discover all the variables available within the scope of a template. Twig provides the dump function for discovering and inspecting variables in template files.

The dump function will not display any output unless debugging is enabled. Learn how to enable Twig debugging.

Once enabled, the dump function can be used to output information about a variable or variables within a template.

Inspecting a single variable

If your template has a title variable available, the following will dump its contents to your template:

{{ dump(title) }}

Discovering all available variables in a template

To dump all available variables and their contents in a template, add the following to your template:

{{ dump() }}

To dump only the available variable keys use:

{{ dump(_context|keys) }}

There are additional global variables available in all Twig templates:

  • _self references the current template and contains advanced information about a template, i.e. the compiled template class name and information about the Twig environment.
  • _context references the current context and contains all variables passed to the template such as variables sent from theme(), prepared by preprocess, or set in the template. Adding {{ dump() }} without specifying a variable is equivalent to {{ dump(_context) }}.
  • _charset references the current charset.

Beware of dump()

If you want to see all variables, but dump() results in exhausted memory because of recursion or the like, you can loop through _context to see the all the keys in it:

<ol>     {% for key, value in _context  %}       <li>{{ key }}</li>     {% endfor %} </ol>

Then use a conditional to check (for example {{ if loop.index = 2 %}), and dump that value only if necessary.

More information

See Twig's dump function documentation to learn more about the dump function.