You will want the combination of trim_blocks
and lstrip_blocks
both being False
. So, the last version you have is actually quite close.
To achieve the desired output though, you will need to make use of whitespace control mechanisms provided by Jinja2. In particular, you will be interested in this:
You can also strip whitespace in templates by hand. If you add a minus
sign (-) to the start or end of a block (e.g. a For tag), a comment,
or a variable expression, the whitespaces before or after that block
will be removed:
Therefore, by adding -
signs in the for
loop and macro
, you can remove the unwanted newlines which occur upon expansion.
In the case of the for
loop, add it to both sides of start and end of the block. However, in the case of the macro
, add it only on one side -- if you add it to both, it will merge the <ul>
and <li>
onto the same line (and similarly for the closing tags).
The following should work for you:
from jinja2 import Environment, FileSystemLoader, Template
template_string = """
{%- macro entry(e) %}
<li>
<a href="{% if 'link' in e %}{{ e.link }}{% endif %}">
<code>{{ e.title }}</code>
</a>{% if e.desc %} - {{ e.desc }}{% endif %}
{% if e.date %}<time datetime="{{ e.date }}"> ({{ e.date.split('-')[0] }})</time>{% endif %}
</li>
{% endmacro -%}
<ul>
{%- for e in entries -%}
{{ entry(e) }}
{%- endfor -%}
</ul>
"""
entries = [
{'title': 'title', 'date': '2020', 'desc': 'desc', 'link': 'link'}
]
env = Environment(trim_blocks=False, lstrip_blocks=False)
template = env.from_string(template_string)
print(template.render(entries=entries))
Which gives the desired output:
<ul>
<li>
<a href="link">
<code>title</code>
</a> - desc
<time datetime="2020"> (2020)</time>
</li>
</ul>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…