如何在包含文件中使用宏 [英] How to use macros in a included file
问题描述
view.jinja
{%extendslayout / defaultlayout.jinja%}
{%include( 'details.jinja')%}
defaultlayout.jinja
{%import'elements / macros.jinja'as html%}
但是,我无法在details.jinja中使用宏 html ,而无需重新包含它
从你的例子看,就好像你试图导入 macros.jinja
,并使用 作为名为 html
的宏。这是不行的。
宏在jinja文件中定义,名字在那里。
macros.jinja:
{%macro dostuff(x,y,z)%}
{%endmacro%}
然后您可以使用导入标记导入整个文件:
{%import macros.jinja as macros%}
$ p $然后,在你当前的命名空间中,你将有
宏
,它指向macros.jinja文件。要使用dostuff
宏,必须调用macros.dostuff(...)
。
您需要在macros.jinja中定义一个名为
html
的宏,将macros.jinja导入为宏
,然后用
macros.html(...)
来调用。
有道理吗?
view.jinja
{% extends "layout/defaultlayout.jinja" %} {% include('details.jinja') %}
defaultlayout.jinja
{% import 'elements/macros.jinja' as html %}
But i am not able to use the macro html in details.jinja without reincluding it
解决方案It looks, from your examples, as if you're trying to import
macros.jinja
, and use that as a macro calledhtml
. It doesn't work like that.Macros are defined within a jinja file, with names there.
macros.jinja:
{% macro dostuff(x,y,z) %} <a href="{{ x }}" title="{{y}}">{{z}}</a> {% endmacro %}
and you can then import whole files with the import tag:
{% import macros.jinja as macros %}
so then, in your current namespace, you will have
macros
, which points to the macros.jinja file. To use thedostuff
macro, you have to callmacros.dostuff(...)
.You need do define a macro called
html
inside macros.jinja, import macros.jinja asmacros
, and then call it withmacros.html(...)
.Does that make sense?
这篇关于如何在包含文件中使用宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!