13

I'm trying to show divs dependent on whether a database entry has been made:

<table class="info-table">
<tr><td>
<div class="info-table_titlebox">
{% if post.wrk_1_title is defined %}
    <div class="info-title_title">
    <h2>{{post.wrk_1_title}}</h2>
    <h3>Facilitator: {{post.wrk_1_facilitator}}</h3>
    <h4>Location: {{post.wrk_1_locate}}</h4>
    <h4>Max participants: {{post.wrk_1_max}}</h4>
    </div>
    <div class="info-title_list">
        <ul>
        <li>{{post.eventday}} - <b>Week {{post.eventweek}}</b></li>
        <li class="info-title_listright">{{post.wrk_1_time}}</li>
        </ul>
    </div>
    <p>{{post.wrk_1_description}}</p>
{% endif %}
</div>
</td>
<td>
<div class="info-table_titlebox">
{% if post.wrk_1_title is defined and post.wrk_2_title is defined %} 
    <div class="info-title_title">
    <h2>{{post.wrk_2_title}}</h2>
    <h3>Facilitator: {{post.wrk_2_facilitator}}</h3>
    <h4>Location: {{post.wrk_2_locate}}</h4>
    <h4>Max participants: {{post.wrk_2_max}}</h4>
    </div>
    <div class="info-title_list">
        <ul>
        <li>{{post.eventday}} - <b>Week {{post.eventweek}}</b></li>
        <li class="info-title_listright">{{post.wrk_2_time}}</li>
        </ul>
    </div>
    <p>{{post.wrk_2_description}}</p>
{% endif %}
</div>
</td>

This is a simplified snippet - the pattern carries on. Basically if the title is in the database show only div1 if both title 1 and title 2 are in the database show div1 and div2 and so on.

Currently this sort of works as it shows the div I want to show but for some reason it shows the next one as well. If I have title for div 1 it shows 1 and 2, if I have a title for div 1 and 2 it shows 1, 2, and 3

I am really confused as I am really new to Jinja2. I am not sure if it's my positioning of the syntax in the html, or if the syntax is wrong, or if you are not able to check across two variables ... any help would be appreciated.

4
  • Are you sure the jinja variables are not defined in the context? You can always test this by splitting the {% if post.wrk_1_title is defined and post.wrk_2_title is defined %} in two if's and look in the resulting html. Commented Sep 5, 2012 at 14:09
  • @vocausa I guess im sorta unsure what defined means, i assumed when an entry is made to the database but nothing is added from the form submission that that means that when this entry is retrieved from the database its "undefined" at first I thought it would come up as "None" but it seems it does make an entry...maybe a empty string? so by checking for "defined" if there is an entry it will show this div. Commented Sep 6, 2012 at 6:54
  • A variable with a value of None is defined. Not defined means: the variable is not known. Commented Sep 6, 2012 at 8:04
  • yeah i finally figure that out, what i ended up doing was creating a procedure that if an empty string was added to the database "None" was entered and then used {% if post.wrk_1_title is not none %} to show the div if there was an appropriate variable recovered Commented Sep 6, 2012 at 8:29

1 Answer 1

39

As in Python, the 0, None, [], {} and "" are False. Anything other than that, it's True.

"The if statement in Jinja is comparable with the if statements of Python. In the simplest form you can use it to test if a variable is defined, not empty or not false:"

{% if post and post.wrk_1_title %}

{% endif %}

Documentation: http://jinja.pocoo.org/docs/templates/#if

Sign up to request clarification or add additional context in comments.

2 Comments

just to confirm I understand the above statement your saying the jinja defined is a test to check if there is a variable??
Yes, but the post should be defined. If you do not know if post is defined, you should test it: post and post.wrk_1_title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.