- Author:
- techiegurl
- Posted:
- May 22, 2008
- Language:
- HTML/template
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
This custom filter takes in a string and breaks it down by newline then makes each separate line an item in an unordered list. Note that it does not add the <ul> </ul> tags to give the user a chance to add attributes to the list. This is based of the 'linebreaks' filter built in django
1 2 3 4 5 6 7 8 9 10 11 | from django import template
import re
register = template.Library()
@register.filter(name='html_list')
def make_html_list(value):
"""Break a string down based on newline characters and for each line, enclose it in the <li> and </li> without the <ul> and </ul> tags.
Similar to the unordered_list filter but not requiring a list"""
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
paras = re.split('\n', value)
paras = ['<li>%s</li>' % p.strip().replace('\n', '<br/>') for p in paras]
return '\n\n'.join(paras)
|
More like this
- Bootstrap Accordian by Netplay4 5 years, 11 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 11 months ago
- Reusable form template with generic view by roldandvg 9 years ago
- Pagination Django with Boostrap by guilegarcia 9 years, 2 months ago
Comments
#
Please login first before commenting.