【问题标题】:Import with access to context (nunjucks)导入并访问上下文 (nunjucks)
【发布时间】:2018-04-06 00:15:24
【问题描述】:

在我的 app.js 中,我有这个:app.locals.testvalue = "lolo"

在我的 macro.html 中,我有这个:{% macro powertestvalue(x) %} <p> {{ x }} : {{ testvalue }}</p>{% endmacro %}

问题是我只能在“macro.html”中使用 powertestvalue 宏,原因如下:

  1. 如果我使用“include”,我将无法使用包含 macro.html 的文件中的 powertestvalue。
  2. 如果我使用“import”,macro.html 将无法访问全局范围,也无法使用 {{ testvalue }}。

我应该如何处理这个问题?

【问题讨论】:

  • 比预期的还要糟糕。我可以访问在父级中定义的宏,但不能在父级的父级中访问。

标签: nunjucks


【解决方案1】:

您可能会发现这很有用:http://jinja.pocoo.org/docs/2.9/templates/#import-context-behavior

试试:

{% from 'macro.html' import powertestvalue with context %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 2011-03-24
    • 2020-02-17
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多