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 5 years, 3 months ago
  2. extending generic_markup.apply_markup by mandric 5 years, 12 months ago
  3. render_markup filter, specify the markup filter as a string by exogen 6 years, 1 month ago
  4. Tags & filters for rendering search results by exogen 5 years, 1 month ago
  5. Skip only specified spaces by axil 1 year, 3 months ago

Comments

spookylukey (on September 4, 2009):

Cheers, I love djangosnippets!

#

(Forgotten your password?)