jinja2 how to remove trailing newline

后端 未结 6 1479
轮回少年
轮回少年 2021-01-31 01:36

I\'m using jinja 2 to output a yaml file but can\'t seem to get rid of a trailing newline and the end of a for loop. Eg the below

 - request:
        path: {{ p         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 02:15

    Change your loop to strip white spaces from the top AND bottom of the output (notice extra "-" at the for loop close):

     {% for key, value in querystring.items() -%}
          {{ key }}: '{{ value }}'
     {%- endfor %}
    

    In my tests (using https://github.com/abourguignon/jinja2-live-parser), the "-" must come after the first {%, not before the last to achieve what you're asking for.

    Docs: http://jinja.pocoo.org/docs/dev/templates/#whitespace-control

提交回复
热议问题