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. FileField having auto upload_to path by junaidmgithub 6 days ago
  2. LazyPrimaryKeyRelatedField by LLyaudet 1 week, 6 days ago
  3. CacheInDictManager by LLyaudet 1 week, 6 days ago
  4. MYSQL Full Text Expression by Bidaya0 2 weeks ago
  5. Custom model manager chaining (Python 3 re-write) by Spotted1270 2 weeks, 6 days ago

Comments

spookylukey (on September 4, 2009):

Cheers, I love djangosnippets!

#

Please login first before commenting.