Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass boolean keyword argument along with the use of "Include" template tag

{% include "example.html" with name="John" hide_last_name=True %}

Basically, I am trying to include "example.html" as a sub-template in my main template. Additional context is provided with the mean of passing the keyword arguments name and hide_last_name. While the django template system has no trouble recognize name, it somehow just can't recognize hide_last_name. I suspect the use of boolean keyword argument in Include tag is now allowed but then I can't find anywhere in the official docs mentions that. Please help out. Thanks.

like image 398
tamakisquare Avatar asked Aug 03 '12 22:08

tamakisquare


1 Answers

For Django <= 1.4.x

As said before, Django tries to find a variable named "True". The simplest way to handle this is to use an integer value, which will not be evaluated.

You could write in the includer template

{% include "example.html" with show_last_name=1 %}

and in the included template

John
{% if show_last_name %}
    Doe
{% endif %}

For Django >= 1.5

You can use True and False in templates, so this is no longer a issue

like image 59
Eloims Avatar answered Oct 04 '22 17:10

Eloims