Frage

I am writing a custom filter which should hightlight the char or chars with <strong> if this char or chars are contained in the given text.

this is my custom filter:

@register.filter(needs_autoescape=True)
def highlight(text, sterm, autoescape=None):
   if autoescape:
       esc = conditional_escape
   else:
       esc = lambda x: x
   result = text.replace(esc(sterm),'<strong>'+esc(sterm)+'</strong>')
   return mark_safe(result)

and in template I {% load %}ed and used in this way:

{{search_result_text|highlight:searchterm}}

The problem is it is highlighting all text which comes after the matched term:

enter image description here

as you see, the first block is what is happening right now. I want to achieve the second block. I searched for level

What is wrong with my code?

EDIT: Sorry, it is my typo. I didnot close <strong> properly, now it is working like a charm! :). I leave this here so other can make use of it.

War es hilfreich?

Lösung

It was my typo: I didnot close <strong> properly inside my filter function.

Now it is working like a charm!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top