gpt4 book ai didi

php - 如何在 TWIG 的宏中访问 _context 变量?

转载 作者:可可西里 更新时间:2023-11-01 12:30:53 25 4
gpt4 key购买 nike

我正在尝试在宏中访问我的一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com