How could I generate the expected output ? Thanks
{%- for field in fields -%}
-
name: {{field}}
type: string
{%- endfor -%}
-
name: operating revenue
type: string-
name: gross operating profit
type: string-
-
name: operating revenue
type: string
-
name: gross operating profit
type: string
from jinja2 import Template
fields = ["operating revenue", "gross operating profit", "EBITDA", "operating profit after depreciation", "EBIT", "date"]
template_file = open('./fields_template.jinja2').read()
template = Template(template_file)
html_rendered = template.render(fields=fields)
print(html_rendered)
The - removes all whitespace between that side of the Jinja tag and the first character. You are using - on the 'inside' of the tags, so whitespace is removed up to the - character and after the word string, joining up the two. Remove one or the other.
You could remove the extra newlines at the start and end of your text for example, and remove the - from the inside side of the opening tag:
{%- for field in fields %}
-
name: {{field}}
type: string
{%- endfor -%}
Demo:
>>> from jinja2 import Template
>>> fields = ["operating revenue", "gross operating profit", "EBITDA", "operating profit after depreciation", "EBIT", "date"]
>>> template_file = '''\
... {%- for field in fields %}
... -
... name: {{field}}
... type: string
... {%- endfor -%}
... '''
>>> template = Template(template_file)
>>> html_rendered = template.render(fields=fields)
>>> print(html_rendered)
-
name: operating revenue
type: string
-
name: gross operating profit
type: string
-
name: EBITDA
type: string
-
name: operating profit after depreciation
type: string
-
name: EBIT
type: string
-
name: date
type: string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With