I know that dashes let you adjust newlines, but I can't figure out where to
put dashes to get the result I want.

I tried adding dashes like this:
    {{ f(x) -}}
    {{ f(x) | indent(0) -}}
    {{ f(x) | indent(4) -}}

That works for the first macro call, but for the second and third:
- The empty list case is fine (I get zero lines of output)
- The non-empty list case is messed up.  I get fewer newlines than I need.

I basically just want a macro to show the elements of a list, one per
line.  Any way to do that?

On Thu, Sep 1, 2016 at 1:11 PM, John Robson <[email protected]> wrote:

> Use dash "{%- xxxx -%}"
>
> http://jinja.pocoo.org/docs/dev/templates/#whitespace-control
>
>
> On 08/26/2016 09:00 PM, Kannan Goundan wrote:
>
>> 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
>> [email protected]
>> <mailto:[email protected]>.
>> To post to this group, send email to
>> [email protected]
>> <mailto:[email protected]>.
>> Visit this group at https://groups.google.com/group/pocoo-libs.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "pocoo-libs" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/to
> pic/pocoo-libs/RsPH2JISwbA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/pocoo-libs.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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