Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does webapp has 'elseif' or 'elif' in template tags

my code is : Hello!~~~

{% if user %}
    <p>Logged in as {{ user.first_name }} {{ user.last_name }}.</p>
{% elif openid_user%}
    <p>Hello, {{openid_user.nickname}}! Do you want to <a href="{{openid_logout_url}}">Log out?</p>
{% else %}
    <p><a href="/login?redirect={{ current_url }}">google Log in</a>.</p>
    <p><a href="/twitter">twitter Log in</a>.</p>
    <p><a href="/facebook">facebook Log in</a>.</p>
    <p><a href="{{openid_login_url}}">openid Log in</a>.</p>
    <iframe src="/_openid/login?continue=/"></iframe>
{% endif %}

the error is :

TemplateSyntaxError: Invalid block tag: 'elif'

does not webapp has a 'else if ' ?

thanks

like image 780
zjm1126 Avatar asked Jun 01 '10 01:06

zjm1126


People also ask

Can we use Elif in Django template?

We can use if elif..else conditionals in django template. The only difference between python code and django template code is it's syntax. It's similar to python code.

Why is {% include %} used?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

Why is {% extends %} tag used?

The extends tag is used to declare a parent template. It should be the very first tag used in a child template and a child template can only extend up to one parent template. To summarize, parent templates define blocks and child templates will override the contents of those blocks.

Is equal in Django template?

If the values of these variable are equal, the Django Template System displays everything between the {% ifequal %} block and {% endifequal %}, where the {% endifequal %} block marks the end of an ifequal template tag.


2 Answers

Update: as Jeff Bauer says in a comment below, Django 1.4 provides an elif tag.

Original answer as follows:


The Django Book has this to say:

There is no {% elif %} tag. Use nested {% if %} tags to accomplish the same thing:

So, you have to do

if
else
  if
  else

to achieve an if-elsif-else.

This is reflected in their Design Philosophies:

The goal is not to invent a programming language. The goal is to offer just enough programming-esque functionality, such as branching and looping, that is essential for making presentation-related decisions.

like image 109
Mark Rushakoff Avatar answered Oct 13 '22 21:10

Mark Rushakoff


webapp per se has no templates, but you can use Django templates - by default, those from back in Django 0.96 (as you see from the ancient docs I pointed to, that requires the nested if to be physically nested inside the else block). You can use a more updated version of Django (see here for more details) by starting your Python code with

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.1')

but while this does give you a template language that's vastly improved in many ways, those improvements do not include an elseif or elif tag -- you still have to explicitly nest the secondary if inside the primary one's else branch.

like image 45
Alex Martelli Avatar answered Oct 13 '22 19:10

Alex Martelli