- Author:
- jheasly
- Posted:
- March 29, 2007
- Language:
- Python
- Version:
- .96
- Tags:
- filter
- Score:
- 1 (after 1 ratings)
Based upon (i.e., mostly lifted from) the built-in "linebreaks" and "linebreaksbr" filters, this one works as they do:
{{ object.text|paragraphs }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from django.template import Library, Node
from django.template.defaultfilters import stringfilter
import re
register = Library()
def paragraphs(value):
"""
Turns paragraphs delineated with newline characters into
paragraphs wrapped in <p> and </p> HTML tags.
"""
paras = re.split(r'[\r\n]+', value)
paras = ['<p>%s</p>' % p.strip() for p in paras]
return '\n'.join(paras)
paragraphs = stringfilter(paragraphs)
register.filter(paragraphs)
|
More like this
- "Magic Link" Management Command by webology 3 weeks, 5 days ago
- Closest ORM models to a latitude/longitude point by simonw 3 weeks, 5 days ago
- Log the time taken to execute each DB query by kennyx46 3 weeks, 5 days ago
- django database snippet by ItsRLuo 1 month ago
- Serialize a model instance by chriswedgwood 2 months ago
Comments
I would simplify
to
#
@Archatas,
Good suggestion. Thanks!
#
buit-in tag in Django: linebreaks
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#linebreaks
#
Oh, sorry this tag add <br> into
#
Please login first before commenting.