Login

Template Filter: Add indentation

Author:
chuck-harmston
Posted:
August 26, 2009
Language:
Python
Version:
1.1
Score:
2 (after 2 ratings)

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" }}
 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. Serializer factory with Django Rest Framework by julio 5 months, 3 weeks ago
  2. Image compression before saving the new model / work with JPG, PNG by Schleidens 6 months, 2 weeks ago
  3. Help text hyperlinks by sa2812 7 months, 1 week ago
  4. Stuff by NixonDash 9 months, 2 weeks ago
  5. Add custom fields to the built-in Group model by jmoppel 11 months, 3 weeks ago

Comments

spookylukey (on September 4, 2009):

Cheers, I love djangosnippets!

#

Please login first before commenting.