Flask 如何在包含的文件中使用宏

Flask 如何在包含的文件中使用宏,flask,jinja2,Flask,Jinja2,view.jinja {% extends "layout/defaultlayout.jinja" %} {% include('details.jinja') %} defaultlayout.jinja {% import 'elements/macros.jinja' as html %} 但是我无法详细使用宏html。如果不重新包含它,我将无法使用宏html

view.jinja

{% extends "layout/defaultlayout.jinja" %}
{% include('details.jinja') %}
defaultlayout.jinja

{% import 'elements/macros.jinja' as html %}

但是我无法详细使用宏html。如果不重新包含它,我将无法使用宏html宏。jinja,并将其用作名为
html
的宏。它不是那样工作的

宏是在一个jinja文件中定义的,其中有名称

macros.jinja:

{% macro dostuff(x,y,z) %}
    <a href="{{ x }}" title="{{y}}">{{z}}</a>
{% endmacro %}
因此,在当前名称空间中,您将拥有指向macros.jinja文件的
macros
。要使用
dostuff
宏,必须调用
macros.dostuff(…)

您需要在macros.jinja中定义一个名为
html
的宏,将macros.jinja导入为
macros
,然后使用
macros.html(…)
调用它


这有意义吗?

丹尼尔的回答对我没有帮助。我必须用下面的方法导入

{% from "post_entity.html" import show_post with context %}
这里的
post_entity.html
文件包含带有
show_post
方法的宏
然后使用以下方法:

{{show_post(post)}}
这里的
post
是从flask
render\u template
发送到模板的字典。
宏文件
文件看起来像这样:
post_entity.html

{% macro show_post(post) %}
    {{post.photo_url}}
    {{ post.caption }}
{% endmacro %}

不记得这是否适用于宏,但请尝试使用上下文
include('details.jinja')
UndefinedError:'macros'未定义引号是必须的:
import“macros.jinja”作为宏
。如果有人指出为什么在上下文中添加
会修复它,以及为什么忽略它会出现错误,那就太好了。。。
{% macro show_post(post) %}
    {{post.photo_url}}
    {{ post.caption }}
{% endmacro %}