【问题标题】:Exporting imported macros导出导入的宏
【发布时间】:2019-08-29 08:45:57
【问题描述】:

我希望能够从一个中心文件导入宏,但将每个宏放在单独的文件中。

我目前的方法是使用macros.twig,它可以导入所有其他宏。

{% from 'foo-macro.twig' import foo %}
{% from 'bar-macro.twig' import bar %}
{% from 'baz-macro.twig' import baz %}

但是在example.twig中导入时:

{% from 'macros.twig' import foo, bar, baz %}

{{ foo('foo arg') }}
{{ bar('bar arg') }}
{{ baz('baz arg') }}

Error: Macro "foo" is not defined in template "macros.twig".

我还尝试通过include 将每个宏包含在macros.twig 中。同样的问题。

我有没有在文档中找不到的方法?

Here's a TwigFiddle

【问题讨论】:

    标签: symfony drupal twig


    【解决方案1】:

    您需要在要使用的每个文件中导入宏。 这已在 Twig 2.0 中更改并已记录在案

    从 Twig 2.0 开始,文件中导入的宏在子模板中不再可用(例如通过 include 调用)。您需要在使用宏的每个文件中显式导入宏。

    source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      相关资源
      最近更新 更多