简体   繁体   English

将{%include%}的输出分配给Jinja2中的变量

[英]Assigning the output of {% include %} to a variable in Jinja2

I have a template that calls a sub template using {% include %}. 我有一个使用{%include%}调用子模板的模板。 I want to assign the rendered output of the subtemplate to a variable in outer template to further modify it with filters. 我想将子模板的渲染输出分配给外部模板中的变量,以使用过滤器进一步对其进行修改。

I get a syntax error if I do the following 如果执行以下操作,则会收到语法错误

Example

Outer_template
    blah
    blah
    ## set expanded_inner_template = {% include "inner_template" %}
    {{ expanded_inner_template|somefilter }}

(Syntax Error: unexpected '%) 

Is there any way to assign the rendered output of sub-template to a variable in outer template for further processing 有没有办法将子模板的渲染输出分配给外部模板中的变量以进行进一步处理

I was in need of something similar and come across your question. 我需要类似的东西,遇到您的问题。

Since you said: 由于您说过:

to further modify it with filters. 用过滤器进一步修改它。

It does not answer your question directly, but if all you want is to modify it in-place with filters, there are filter sections . 它不会直接回答您的问题,但是如果您只想使用过滤器对其进行修改,则可以使用过滤器部分

{% filter somefilter %}
    {% include "inner_template" %}
{% endfilter %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM