Login

Tag "if"

Snippet List

Django 1.2 template tag {% IF %} with {% ELIF %} support

Adds to Django 1.2 tag `{% elif %}` {% if user.nick == "guest" %} Hello guest! {% elif user.nick == "admin" or user.is_admin %} Hello admin! {% elif user %} You are registered user {% else %} Login to site {% endif %} Snipped designed for [gaeframework.com](http://www.gaeframework.com) Inspired by snippets: [#1572](http://djangosnippets.org/snippets/1572/) and [#2243](http://djangosnippets.org/snippets/2243/)

  • template
  • if
  • elif
  • django 1.2
  • tags. tag
Read More

Easy Conditional Template Tags

This is a conditional templatetag decorator that makes it *very* easy to write template tags that can be used as conditions. This can help avoid template boilerplate code (e.g. setting a variable in your template to be used in a condition). All you have to do is define a function with expected parameters that returns True or False. Examples are in the code.

  • template
  • tag
  • templatetag
  • if
  • conditional
  • condition
  • else
  • endif
Read More

Smart {% if %} template tag

Save this as `smart_if.py` in the `templatetags` folder of one of your apps. Then a simple `{% load smart_if %}` replaces the boring built-in Django `{% if %}` template with the new smart one. *7 May 2009*: Was asked about whether it handles combination of and/or. It does, added a test to show it. I actually like how Django doesn't let you do this, but I'm not going to confuscate my code for a restriction like this. *15 June 2009*: Fixed up a bug with boolean precedence (`x or x == 0` was being parsed as `(x or x) == 0` instead of `x or (x == 0)`). Add some extra test cases, including some for invalid cases.

  • if
  • smart-if
  • if-in
  • greater-than
  • less-than
Read More

ifcontains tag

Django does not have ability to write {% if "item" in list %}, so I had to write this tag. It can be used just like ifequal tag

  • tag
  • if
  • ifcontains
  • block-tag
Read More

If modulo template tag

Usage: {% ifmodulo forloop.counter 4 0 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %} or {% ifnotmodulo 5 3 %} <!-- do something --> {% else %} <!-- do something else --> {% endifmodulo %} When the third parameter does not exist, it is assumed you're checking for values different than 0.

  • template
  • tag
  • if
  • modulo
Read More

PyIf Template Tag (Conditional Tag)

Cheers to limodou for getting me thinking about this. The only problem with his implementation is that it doesn't support Django's "." syntax for accessing array/dict elements. In the Django style of allowing simple syntax for designers while allowing for greater flexibility, and less template duplication for conditionals that were previously impossible to represent in templates, I modified Django's built-in If tag. This is an adaptation/enhancement to Django's built in IfNode {% if ... %} that combines if ifequal ifnotequal into one and then adds even more. This Supports 1. ==, != 2. not .... 3. v in (1,"y",z) 4. <=, <, >=, > 5. nesting (True and (False or (True or False))) How to use it: {% pyif i == 1 or (5 >= i and i != 7) and user.first_name in ('John', 'Jacob') %} 'Tis true. {% else %} 'Tis false. {% endif %} I hope you like it.

  • template
  • tag
  • templatetag
  • if
  • conditional
  • ifequal
  • ifnotequal
Read More

8 snippets posted so far.