highlight text
How to use: {% load highlight %} {{ text|highlight:'word' }}
- text
- highlighting
- highlight
How to use: {% load highlight %} {{ text|highlight:'word' }}
Encloses all matches of a pattern between the opentag and closetag string. ` {% with "this is a large test" as a %} {{ a|highlightpattern:"a" }} {% endwith %} ` yields this is <b>a</b> l<b>a</b>rge test
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.
Use these tags and filter when you're rolling your own search results. This is intended to be a whole templatetags module. I keep it in my apps as `templatetags/search.py`. These should not be used to perform search queries, but rather render the results. ### Basics There are three functions, each has both a tag *and* a filter of the same name. These functions accept, at a minimum, a body of text and a list of search terms: * **searchexcerpt**: Truncate the text so that each search term is shown, surrounded by some number of words of context. * **highlight**: Wrap all found search terms in an HTML span that can be styled to highlight the terms. * **hits**: Count the occurrences of the search terms in the text. The filters provide the most basic functionality as described above, while the tags offer more options as arguments, such as case sensitivity, whole word search, and saving the results to a context variable. ### Settings Defaults for both the tags and filters can be changed with the following settings. Note that these settings are merely a convenience for the tags, which accept these as arguments, but are necessary for changing behavior of the filters. * `SEARCH_CONTEXT_WORDS`: Number of words to show on the left and right of each search term. Default: 10 * `SEARCH_IGNORE_CASE`: False for case sensitive, True otherwise. Default: True * `SEARCH_WORD_BOUNDARY`: Find whole words and not strings in the middle of words. Default: False * `SEARCH_HIGHLIGHT_CLASS`: The class to give the HTML span element when wrapping highlighted search terms. Default: "highlight" ### Examples Suppose you have a list `flatpages` resulting from a search query, and the search terms (split into a list) are in the context variable `terms`. This will show 5 words of context around each term and highlight matches in the title: {% for page in flatpages %} <h3>{{ page.title|highlight:terms }}</h3> <p> {% searchexcerpt terms 5 %} {{ page.content|striptags }} {% endsearchexcerpt %} </p> {% endfor %} Add highlighting to the excerpt, and use a custom span class (the two flags are for case insensitivity and respecting word boundaries): {% highlight 1 1 "match" %} {% searchexcerpt terms 5 1 1 %} {{ page.content|striptags }} {% endsearchexcerpt %} {% endhighlight %} Show the number of hits in the body: <h3>{{ page.title }} (Hits: {{ page.content|striptags|hits:terms }}) </h3> All tags support an `as name` suffix, in which case an object will be stored in the template context with the given name; output will be suppressed. This is more efficient when you want both the excerpt and the number of hits. The stored object depends on the tag: * **searchexcerpt**: A dictionary with keys "original" (the text searched), "excerpt" (the summarized text with search terms), and "hits" (the number of hits in the text). * **searchcontext**: A dictionary with keys "original", "highlighted", and "hits", with obvious values. * **hits**: Just the number of hits, nothing special. Getting both the hits and the excerpt with "as": {% searchexcerpt terms 3 as content %} {{ page.content|striptags }} {% endsearchexcerpt %} <p>Hits: {{ content.hits }}<br>{{ content.excerpt }}</p> ### More For more examples see [Brian Beck's Text Adventure][announcement]. [announcement]: http://blog.brianbeck.com/post/29707610
4 snippets posted so far.