I basically want to do something like this in my base template:
{% if the block 'headline' is not empty %}
<div class="something"><h1>{% block headline %}{% end block %}</h1></div>
{% endif %}
In jinja2 it appears blocks are not variables and you can't get at their contents or test their values, or anything else but output them.
This seems like it would be a no-brainer to allow this, but I can't figure out a way. Do I have to use macros instead of blocks?
You should be able check the contents of a block using the self.blockname
syntax.
{% if self.headline() is not empty %}
{# Write out Headline HTML wrapper here #}
{% endif %}
To quote from the documentation :
If you want to print a block multiple times you can however use the special self variable and call the block with that name:
<title>{% block title %}{% endblock %}</title>
<h1>{{ self.title() }}</h1>
{% block body %}{% endblock %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.