I'd like to detect if a Jinja2 template block content is empty or not. Something like this:
{% block foo %}{% endblock foo %}{% if foo %} - {% endif %}Blah Blah Blah
What I want is conditional text outside the block definition itself. In the contrived example, I want to be able to insert a conditional string -
after the block if and only if the block has been overridden and is not empty.
Is this possible?
All the block tag does is tell the template engine that a child template may override those placeholders in the template. In your example, the base template (header. html) has a default value for the content block, which is everything inside that block. By setting a value in home.
Simply call the block:
{% if self.foo() %} - {% endif %}
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