如何创建jinja2扩展,用with节点包装Include

如何创建jinja2扩展,用with节点包装Include,jinja2,Jinja2,我试图编写一个jinja2扩展来模拟由with语句包装的include语句的行为 在这样的背景下: context={ “章节”:{ “A节”:{ “你好”:“世界” } } } 我想包括以下模板部分A.tpl print my {{section.hello}} 使用此自定义扩展名 {% section "sectionA" %} 哪个应该效仿 {% with section=sections.sectionA %} {% include 'secti

我试图编写一个jinja2扩展来模拟由with语句包装的include语句的行为

在这样的背景下:

context={
“章节”:{
“A节”:{
“你好”:“世界”
}
}
}
我想包括以下模板部分A.tpl


print my {{section.hello}}

使用此自定义扩展名


{% section "sectionA" %}

哪个应该效仿


{% with section=sections.sectionA %}
   {% include 'sectionA.tpl' %}
{% endwith %}

我能够模拟include扩展(基本上是复制解析器模块的parse_include函数),但是我不能修改上下文,因为它是只读的,所以我认为可以通过节点创建对象。你知道怎样才能做到吗