Login

linebreaksli template filter

Author:
rokclimb15
Posted:
June 20, 2007
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

This template filter will split a string of text on newlines and return a string of <li></li>s with a newline before every line.

This is handy for taking a paragraph of text and making an <ol> or <ul> from its lines.

Don't forget to register your filter with the template library first or the filter won't work.

1
2
3
4
5
6
7
8
import re, string

def linebreaksli(value):
    "Converts strings with newlines into <li></li>s"
    value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
    lines = re.split('\n', value)
    lines = ['<li>%s</li>' % line for line in lines]
    return '\n'.join(lines) 

More like this

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

Comments

Please login first before commenting.