作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试在宏中访问我的一个 twig 变量。我知道我不能直接这样做。
as with PHP functions, macros don't have access to the current template variables
但相同的页面状态:
You can pass the whole context as an argument by using the special _context variable.
将 _context 传递给宏以及在宏中访问它的语法是什么?
谢谢
最佳答案
考虑以下示例:
1) 在当前上下文中创建一个变量
{% set x = 42 %}
2) 声明一个以对象为参数的宏
{% macro test(variables) %}
variable x = {{ variables.x | default('undefined') }}
{% endmacro %}
3) 使用特殊的_context
对象调用您的宏
{{ _self.test(_context) }}
这将显示:
variable x = 42
关于php - 如何在 TWIG 的宏中访问 _context 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20367170/
我是一名优秀的程序员,十分优秀!