Search code examples
pythonjinja2

how to make jinja2 remove newlines/spaces generated by tags but preserve newlines/spaces generated by static text


When using a jinja2 template such as this

A
{%- if flag == "0" %}
X
{%- elif flag == "1" %}
Y
{%- endif %}
B

my intuitive goal would be, whatever A,B,X,Y are, to simply "insert the contents X,Y between A,B".

This means I'd expect for flag=0,1,2 the following results:

A
X
B
A
Y
B
A
B

Note that A,B,X,Y are not jinja2 variables, they are placeholders for static text in the template, which, however, could also consist of multiple lines.

I seek for a general syntax which satisfies my above expectation for any A,X,Y,B, i.e. if I edit the contents of the fields A,X,Y,B I do not want to touch the syntax.

The syntax given above does not fulfill this requirement, if the fields A,B,X,Y have newlines in their surrounding, e.g. if I change it to

a
{%- if flag == "0" %}
x
{%- elif flag == "1" %}
  y
 y

{%- endif %}
b

(now a,b,x,y are real characters, placeholder Y has multiple lines and ends with an empty line) it will produce for flag=1 the output

a
  y
 y
b

while according to my expectation I want this output

a
  y
 y

b

As I work with a newline-senstive target-language, I would now have to adopt the template syntax again.

So I seek a general template syntax of the following form

A
{????}
X
{????}
Y
{????}
B

which works for any placeholders A,B,X,Y. In this form, jinja2 tags and target placeholders are required to be on separate lines.

What I tried so far:

Playing with my initial template (switching 6 minus signs and adding some newlines at 4 positions) I generated 2^10 = 1024 possible templates. 10 of them only work for A,B,X,Y without newlines, with newlines, none of them does.

The problem seems to be, that using {%- syntax after Y removes the newline before the { (wanted) but at the same time removes the trailing spaces of Y (not wanted)

Considering the question title, I know that it is not clearly distinguishable if a newline is "caused" by a tag or static text, but this was the best I could come up with.


Solution

  • I want jinja2 syntax and target language code on separate lines is

    Use lstrip_blocks=true, trim_blocks=true options.

    Without them, it is not possible. Patch jinja2 or use something else.


    That would be:

    A
    {% if flag == "0" %}X
    {% elif flag == "1" %}Y
    {% endif %}B
    

    and:

    a
    {% if flag == "0" %}x
    {% elif flag == "1" %}  y
     y
    
    {% endif %}b
    

    I sometimes do:

    a
    {% if flag == "0" 
    %}x
    {% elif flag == "1" 
    %}  y
     y
    
    {% endif 
    %}b
    

    Bottom line you are searching for lstrip_blocks=true, trim_blocks=true jinja2 template options.