I'm using Jinja2 to generate Nginx configuration files.  I want to make 
sure the output is readable, and part of that involves being able to be 
precise with blank spaces.

I'm having trouble figuring out how to avoid extra blank spaces when using 
a macro.  Here's a short example:

    import jinja2

    print "Jinja2 version", jinja2.__version__

    TEMPLATE = r'''
    {% macro f(l) -%}
    {% for e in l %}
    > {{ e }}
    {% endfor %}
    {%- endmacro %}
    -- inline --
    {% for e in x %}
    > {{ e }}
    {% endfor %}
    -- f(x) --
    {{ f(x) }}
    -- f(x) | indent(0) --
    {{ f(x) | indent(0) }}
    -- f(x) | indent(4) --
        {{ f(x) | indent(4) }}
    --
    '''

    t = jinja2.Template(TEMPLATE,
                        trim_blocks=True,
                        lstrip_blocks=True,
                        keep_trailing_newline=True)
    print "====== [1, 2] ======"
    print t.render(x=[1, 2])
    print "====== [] ======"
    print t.render(x=[])

And here's the output:

    Jinja2 version 2.8
    ====== [1, 2] ======

    -- inline --
    > 1
    > 2
    -- f(x) --
    > 1
    > 2

    -- f(x) | indent(0) --
    > 1
    > 2
    -- f(x) | indent(4) --
        > 1
        > 2
    --

    ====== [] ======

    -- inline --
    -- f(x) --

    -- f(x) | indent(0) --

    -- f(x) | indent(4) --
        
    --

When the list "x" is non-empty, "f(x)" leaves an extra blank line.  This is 
strange, but there's a simple workaround: "f(x) | indent(0)".

When the list "x" is empty, I can't get the blank line to go away.  The 
only way it works right is if I don't use a macro.

I've tried adding newline suppression marks ("-") in various places but 
couldn't get it to work.

I tried surrounding the macro call with a useless "if":

    {% if 1 %}{{ f(x) }}{% endif %}

That eliminated the blank line in the empty list case, but screwed up the 
indentation in the non-empty list case.

Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pocoo-libs+unsubscr...@googlegroups.com.
To post to this group, send email to pocoo-libs@googlegroups.com.
Visit this group at https://groups.google.com/group/pocoo-libs.
For more options, visit https://groups.google.com/d/optout.

Reply via email to