- Author:
- girasquid
- Posted:
- December 10, 2008
- Language:
- Python
- Version:
- 1.0
- Score:
- 0 (after 0 ratings)
This filter can be used to wrap <span class='highlight'> around anything you want to highlight in a block of text. For example, if you had 'foo bar foo baz' inside the context variable MYTEXT, you could do {{ MYTEXT|highlight:'foo' }}, and "<span class='highlight'>foo</span> bar <span class='highlight'>foo</span> baz" would be returned.
How you style the highlight class is up to you.
1 2 3 | @register.filter
def highlight(text, word):
return mark_safe(text.replace(word, "<span class='highlight'>%s</span>" % word))
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Great solution! Thank you!
#
Please login first before commenting.