Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a Jinja2 template block is empty?

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?

like image 734
Inactivist Avatar asked Sep 10 '13 13:09

Inactivist


People also ask

What is block content in Jinja?

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.


1 Answers

Simply call the block:

{% if self.foo() %} - {% endif %} 
like image 76
Sean Vieira Avatar answered Oct 04 '22 09:10

Sean Vieira