Analogue template filter to removetags that also removes the content of the tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import re
from django.template.defaultfilters import stringfilter
from django.template import Library

register = Library()

@register.filter
@stringfilter
def killtags(value, tags):
    tags = [re.escape(tag) for tag in tags.split()]
    tags_re = u'(%s)' % u'|'.join(tags)    
    kill_re = re.compile("<\s*%s[^>]*>(.*?)<\s*/\s*\\1>" % tags_re, re.U)
    value = kill_re.sub('', value)
    return value
killtags.is_safe = True

More like this

  1. Enhanced "avoid widows" template filters by SmileyChris 5 years, 10 months ago
  2. Tags & filters for rendering search results by exogen 5 years, 2 months ago
  3. Soft-wrap long lines by Ubercore 5 years, 1 month ago
  4. filter for extracting a number of paragraphs from any HTML code by rafadev 1 year, 11 months ago
  5. alientag by itchyfingrs 1 year, 7 months ago

Comments

(Forgotten your password?)