jinja2 how to remove trailing newline

后端 未结 6 1473
轮回少年
轮回少年 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:29

    You can suppress rendering of the below lines:

    <% for ... %>
    <% endfor %>
    <% if ... %>
    <% endif %>
    

    by setting trim_blocks=True and lstrip_blocks=True in your jinja2 environment. See the example below, info from their docs

    context = {'querystring': querystring, 'path': path, 'content': content}    
    jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates/'), trim_blocks=True, lstrip_blocks=True)
    print(jinja_env.get_template('my_template.yaml').render(context))
    

提交回复
热议问题