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. HTML Prettifier by Eloff 4 years ago
  2. MarkupTextField by myles 3 years, 3 months ago
  3. extending generic_markup.apply_markup by mandric 4 years, 8 months ago
  4. typygmentdown by ubernostrum 4 years, 5 months ago
  5. render_markup filter, specify the markup filter as a string by exogen 4 years, 10 months ago

Comments

spookylukey (on September 4, 2009):

Cheers, I love djangosnippets!

#

(Forgotten your password?)