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. Python-like string interpolation in Javascript by phxx 1 year, 7 months ago
  2. testdata tag for templates by showell 2 years, 9 months ago
  3. Front end admin toolbar by eallik 2 years, 2 months ago
  4. linebreaksli template filter by rokclimb15 4 years, 7 months ago
  5. Name Capitalize Filter by hotani 4 years, 9 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?)