如何在包含文件中使用宏 [英] How to use macros in a included file

查看:151
本文介绍了如何在包含文件中使用宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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%} 
宏,它指向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 called html. 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 the dostuff macro, you have to call macros.dostuff(...).

You need do define a macro called html inside macros.jinja, import macros.jinja as macros, and then call it with macros.html(...).

Does that make sense?

这篇关于如何在包含文件中使用宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆