jinja2 trailing newline

16 views
Skip to first unread message

Dmitry Melnikov

unread,
Sep 11, 2018, 5:49:13 AM9/11/18
to pocoo-libs
Hello, I have a template
 
template.jinja2
{% macro print(files) %}
   {% for file in files %}
      {% if file == "a" %}
      file_a
      {% else %}
      file_b
      {% endif %}
   {% endfor %}
{% endmacro %} 
 
BEGIN
      FIRST_FILE
{{print(files)}}
      LAST_FILE
END

which processed with jinja2 as
import jinja2
import os
tmpl_loader = jinja2.FileSystemLoader(searchpath=os.path.dirname("template.jinja2"))
tmpl_env = jinja2.Environment(loader=tmpl_loader, lstrip_blocks=True, trim_blocks=True)
tmpl_file = tmpl_env.get_template(os.path.basename("template.jinja2"))
tmpl_file.stream({'files': ['a', 'b', 'c', 'a']}).dump("file.txt")

 an output 
BEGIN
      FIRST_FILE
      file_a
      file_b
      file_b
      file_a 
                               <------- redundant newline
      LAST_FILE
END

how to remove redundant newline?
I tried various combinations of {%- and -%} without success.
Reply all
Reply to author
Forward
0 new messages