Template Filter: Add indentation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from django.template.defaultfilters import stringfilter
from django import template

register = template.Library()

@register.filter(name='indent')
@stringfilter
def indent(value, arg=1):
    """
    Template filter to add the given number of tabs to the beginning of 
    each line. Useful for keeping markup pretty, plays well with Markdown.
    
    Usage: 
    {{ content|indent:"2" }}
    {{ content|markdown|indent:"2" }}
    """
    import re
    regex = re.compile("^", re.M)
    return re.sub(regex, "\t"*int(arg), value)

More like this

  1. typygmentdown by ubernostrum 6 years, 8 months ago
  2. HTML Prettifier by Eloff 6 years, 2 months ago
  3. MarkupTextField by myles 5 years, 5 months ago
  4. utf8-friendly dumpdata management command (no escape symbols) by dirol 3 years, 5 months ago
  5. MarkupField by jpt 5 years, 1 month ago

Comments

spookylukey (on September 4, 2009):

Cheers, I love djangosnippets!

#

(Forgotten your password?)