I have a simple jinja2 template:
{% for test in tests %}
{{test.status}} {{test.description}}:
{{test.message}}
Details:
{% for detail in test.details %}
{{detail}}
{% endfor %}
{% endfor %}
Which work really good when all of variable of 'test' object are defined like here:
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('my_package', 'templates'), trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True)
template = env.get_template('template.hbs')
test_results = {
'tests': [
{
'status': 'ERROR',
'description': 'Description of test',
'message': 'Some test message what went wrong and something',
'details': [
'First error',
'Second error'
]
}
]
}
output = template.render(title=test_results['title'], tests=test_results['tests'])
Then output looks like this:
ERROR Description of test:
Some test message what went wrong and something
Details:
First error
Second error
But sometimes it is possible that 'test' object will not have 'message' property and in this case there is an empty line:
ERROR Description of test:
Details:
First error
Second error
Is it possible to make this variable stick to whole line? to make it disappear when variable is undefined?