Template Filter: Add indentation

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

fromdjango.template.defaultfiltersimportstringfilterfromdjangoimporttemplateregister=template.Library()@register.filter(name='indent')@stringfilterdefindent(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" }} """importreregex=re.compile("^",re.M)returnre.sub(regex,"\t"*int(arg),value)