Jinja 模板在条件不满足时添加额外的线条
原标题:jinja template adds extra line when if condition is not satisfied
A for loop in my jinja template is like this
{% for m in grp %}
abc {{ m.length }}
pqr
xyz
{% if m.flag is defined and m.flag == "f" %}
yes f {% endif %}
{% for r in uv %}
abcdef
{% endfor %}
{% endfor %}
Now the problem is in some members of grp don t have the flag variable. Wherever flag is present, the option true line is getting added properly. But when if condition is not satisfied, it just adds one blank line. These 4 or 5 lines are supposed to be without extra blank lines otherwise the generated config file gets marked as invalid. Can anyone please help me with this?
最佳回答
Q: A blank line is added if the condition is not satisfied.
A: See Whitespace Control. Quoting:
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.
The template below does what you want
{% if m.flag is defined and m.flag == "f" %}
yes f
{% endif -%}
For example, given the data
grp:
- {length: 1, flag: x}
- {length: 2}
- {length: 3, flag: f}
- {length: 4, flag: f}
The simplified template from your question
{% for m in grp %}
{{ m.length }}
xyz
{% if m.flag is defined and m.flag == "f" %}
yes f {% endif %}
{% endfor %}
adds the blank lines
1
xyz
2
xyz
3
xyz
yes f
4
xyz
yes f
If you use the minus sign
{% for m in grp %}
{{ m.length }}
xyz
{% if m.flag is defined and m.flag == "f" %}
yes f
{% endif -%}
{% endfor %}
there are no blank lines
1
xyz
2
xyz
3
xyz
yes f
4
xyz
yes f
The condition can be simplified. The template below gives the same result
{% for m in grp %}
{{ m.length }}
xyz
{% if m.flag|default( ) == "f" %}
yes f
{% endif -%}
{% endfor %}
Example of a complete playbook for testing
- hosts: localhost
vars:
grp:
- {length: 1, flag: x}
- {length: 2}
- {length: 3, flag: f}
- {length: 4, flag: f}
tasks:
- debug:
msg: |
{% for m in grp %}
{{ m.length }}
xyz
{% if m.flag is defined and m.flag == "f" %}
yes f {% endif %}
{% endfor %}
tags: t1
- debug:
msg: |
{% for m in grp %}
{{ m.length }}
xyz
{% if m.flag is defined and m.flag == "f" %}
yes f
{% endif -%}
{% endfor %}
tags: t2
- debug:
msg: |
{% for m in grp %}
{{ m.length }}
xyz
{% if m.flag|d( ) == "f" %}
yes f
{% endif -%}
{% endfor %}
tags: t3
问题回答
暂无回答
相关问题
How do I access session data in Jinja2 templates (Bottle framework on app engine)?
I m running the micro framework Bottle on Google App Engine. I m using Jinja2 for my templates. And I m using Beaker to handle the sessions. I m still a pretty big Python newbie and am pretty ...
Strip whitespace in generated HTML using pure Python code
I am using Jinja2 to generate HTML files which are typically very huge in size. I noticed that the generated HTML had a lot of whitespace. Is there a pure-Python tool that I can use to minimize this ...
In Jinja2, how can I use macros in combination with block tags?
I m a front end developer, and I ve been trying to get a hang on using Jinja2 effectively. I want to tweak a current site so it has multiple base templates using inheritance, it fully uses block tags ...
Getting translation strings for jinja2 templates integrated with django 1.x?
I can use jinj2 templates with django via render_to_response defined as below
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.http import ...
String concatenation in Jinja
I just want to loop through an existing list and make a comma delimited string out of it.
Something like this: my_string = stuff, stuff, stuff, stuff
I already know about loop.last, I just need to ...
How do you sort a list in Jinja2?
I am trying to do this:
{% for movie in movie_list | sort(movie.rating) %}
But that s not right...the documentation is vague...how do you do this in Jinja2?
How can I access response.context when testing a Jinja2 powered Django view
When I use the Django test.client and I do something like:
class MyTestCase(TestCase):
def test_this(self):
c = self.client
response = c.get( / )
assert False, response....
How to get django context automatically in Jinja2 filters?
For example, I have an paginator object with a lot of attributes, and don t want do write something like {{ paginate(paginator) }} in templates.
How can a get context automatically in the filter ...