Verbose template filter : avoid too many if

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django import template                  

register = template.Library()

@register.filter
def verbose(value, arg):
    try:    
        if not value:
            return ''
        return arg % value
    except Exception:
        return str(value)
 

More like this

  1. Sending html emails with images using Django templates by sleytr 5 years, 11 months ago
  2. Generic model filter from request GET data by genbit 1 year, 9 months ago
  3. linebreaksli template filter by rokclimb15 5 years, 11 months ago
  4. testdata tag for templates by showell 4 years ago
  5. icon shortcut - pseudohtml tag with attribute merging and variables resolving by wiz 2 years, 11 months ago

Comments

marvoloe (on June 12, 2008):

Great idea.

#

rachid (on June 12, 2008):

perfect!

#

carljm (on June 16, 2008):

Good stuff, very handy. If used for HTML as in the last example, don't forget to tag on the |safe filter, too.

#

(Forgotten your password?)