如何将 if 语句放入 jinja2 的宏中?
How can I put if statement inside a macro in jinja2?
我有下面的宏,如果可能的话,我想在其中放置 if 语句。
{% macro input(name, plc, required=true, type='text', class='validate') -%}
<input name="{{ name }}" placeholder="{{ plc }}" type="{{ type }}" class="{{ class }}" value="{{ session.get(name) }}" { % if required %} required {% endif %}>
{%- endmacro %}
因此,它会抛出以下错误:
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endmacro'. The innermost block that needs to be closed is 'macro'.
我猜是打错了,{ % if required %}
里面多了一个白字,应该是{% if required %}
我有下面的宏,如果可能的话,我想在其中放置 if 语句。
{% macro input(name, plc, required=true, type='text', class='validate') -%}
<input name="{{ name }}" placeholder="{{ plc }}" type="{{ type }}" class="{{ class }}" value="{{ session.get(name) }}" { % if required %} required {% endif %}>
{%- endmacro %}
因此,它会抛出以下错误:
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endmacro'. The innermost block that needs to be closed is 'macro'.
我猜是打错了,{ % if required %}
里面多了一个白字,应该是{% if required %}