如何覆盖Jinja2中的嵌套块

时间:2022-01-27 20:47:29

If I define a block inside a block in a Jinja template, and extend it, How do I reference the nested block in the child template?

如果我在Jinja模板的块中定义一个块并扩展它,我如何引用子模板中的嵌套块?

1 个解决方案

#1


9  

You reference the nested block the same way you reference any block, e.g., given

您引用嵌套块的方式与引用任何块的方式相同,例如给定

{% block outer_block %} 
  Outer things
  {% block inner_block %}
    Inner things
  {% endblock %}
  More outer things
{% endblock %}

You'd override inner_block with

你会覆盖inner_block

{% block inner_block %} 
   customized inner content
{% endblock %} 

Can you clarify what problem you're encountering? Or are you running into scoping issues, e.g., http://jinja.pocoo.org/docs/templates/#block-nesting-and-scope ?

你能说清楚你遇到了什么问题吗?或者您是否遇到了范围问题,例如,http://jinja.pocoo.org/docs/templates/#块嵌套和范围?

#1


9  

You reference the nested block the same way you reference any block, e.g., given

您引用嵌套块的方式与引用任何块的方式相同,例如给定

{% block outer_block %} 
  Outer things
  {% block inner_block %}
    Inner things
  {% endblock %}
  More outer things
{% endblock %}

You'd override inner_block with

你会覆盖inner_block

{% block inner_block %} 
   customized inner content
{% endblock %} 

Can you clarify what problem you're encountering? Or are you running into scoping issues, e.g., http://jinja.pocoo.org/docs/templates/#block-nesting-and-scope ?

你能说清楚你遇到了什么问题吗?或者您是否遇到了范围问题,例如,http://jinja.pocoo.org/docs/templates/#块嵌套和范围?