Jinja2 correctly indent included block

前端 未结 3 1654
深忆病人
深忆病人 2021-02-02 07:35

I have two files:

base.html



    
       
       

        
3条回答
  •  花落未央
    2021-02-02 07:57

    Although I think the increase in document size is a good argument against "correctly" indenting documents, jinja2 actually provides a function to do just what you what:

    indent(s, width=4, indentfirst=False)
    

    which you would use in your base.html with a macro, as described in this answer on SO.

    I would probably make that a macro anyway and call it render_register_form:

    {% macro render_register_form(form) %}
        

    Register

    {{ form.hidden_tag() }} {{ form.login.label }} {{ form.login(size=20) }} {{ form.password.label }} {{ form.password(size=20) }}
    {% endmacro %}

    and then include it where needed, for example, with 8 spaces indentation, as:

    {{ render_register_form(my_form)|indent(8, True) }}
    

提交回复
热议问题