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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

spookylukey (on September 4, 2009):

Cheers, I love djangosnippets!

#

Please login first before commenting.