Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 correctly indent included block

Tags:

flask

jinja2

I have two files:

base.html

<!DOCTYPE html> <html>     <head>        <meta charset="UTF-8">        <title>{{title}}</title>     </head>     <body>        {% block content %}        {% endblock %}        </body> </html> 

register.html

{% extends "base.html" %} {% block content %} <h1>Register</h1> <form action="" method="post" name="register">     {{ form.hidden_tag() }}     {{ form.login.label }} {{ form.login(size=20) }}     {{ form.password.label }} {{ form.password(size=20) }}     <input type="submit" value="Register"> </form> {% endblock %} 

It gets rendered like this:

<!DOCTYPE html> <html>     <head>        <meta charset="UTF-8">        <title>Register</title>     </head>     <body>  <h1>Register</h1> <form action="" method="post" name="register">     <div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1393257771.168161##ce877b3519f192c05d3b409f3b7b07bb147dead7"></div>     <label for="login">login</label> <input id="login" name="login" size="20" type="text" value="">     <label for="password">password</label> <input id="password" name="password" size="20" type="password" value="">     <input type="submit" value="Register"> </form>      </body> </html> 

I would like to achieve this:

<!DOCTYPE html> <html>     <head>        <meta charset="UTF-8">        <title>Register</title>     </head>     <body>         <h1>Register</h1>         <form action="" method="post" name="register">             <div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1393257771.168161##ce877b3519f192c05d3b409f3b7b07bb147dead7"></div>             <label for="login">login</label> <input id="login" name="login" size="20" type="text" value="">             <label for="password">password</label> <input id="password" name="password" size="20" type="password" value="">             <input type="submit" value="Register">         </form>     </body> </html> 

Am I missing something? I tried Google and messing with the template indentations, also indent filter does not seem to apply here. I do not want to hard-code the indentation in the inner block, as that would break if I decide to change the formatting and elements in the base later.

like image 447
Fenikso Avatar asked Feb 24 '14 15:02

Fenikso


People also ask

Does Jinja care about indentation?

Indentation inside of Jinja2 blocksJinja2 doesn't care about extra spaces inside of the if or for blocks, it will simply ignore them. It only concerns itself with whitespaces that it finds outside of blocks.

What is block content in Jinja?

The Jinja documentation about templates is pretty clear about what a block does: All the block tag does is tell the template engine that a child template may override those placeholders in the template.


1 Answers

I know, the question is rather old but I found a nice solution to this problem. You can apply filters to whole text blocks: Template Designer Documentation / Filters

{% filter indent(width=4) %} {% include "./sub-template.yml.j2" %} {% endfilter %} 
like image 61
M. Scho. Avatar answered Oct 03 '22 22:10

M. Scho.