2010-07-01 51 views
13

有沒有辦法讓django-haystack的{% highlight %}模板標籤顯示傳入的完整變量,而不是在第一次匹配之前刪除所有內容?django乾草堆高亮模板標籤問題

我使用的是這樣的:

{% highlight thread.title with request.GET.q %} 
+0

5年後,我也有同樣的問題。 Github上甚至有一個問題:https://github.com/django-haystack/django-haystack/issues/748 – weeheavy 2015-07-25 16:10:04

回答

9

我從未使用過乾草堆,而是從快速瀏覽一下在the docsthe source看起來你可以讓自己的自定義熒光筆,並告訴草垛使用這不是

from haystack.utils import Highlighter 
from django.utils.html import strip_tags 

class MyHighlighter(Highlighter): 
    def highlight(self, text_block): 
     self.text_block = strip_tags(text_block) 
     highlight_locations = self.find_highlightable_words() 
     start_offset, end_offset = self.find_window(highlight_locations) 

     # this is my only edit here, but you'll have to experiment 
     start_offset = 0 
     return self.render_html(highlight_locations, start_offset, end_offset) 

,然後設置

HAYSTACK_CUSTOM_HIGHLIGHTER = 'path.to.your.highligher.MyHighlighter' 

在你的settings.py

2

通過@second作品的答案,但是如果你也不想它切斷字符串的結尾,你是最大長度下,你可以試試這個。仍然測試它,但它似乎工作:

class MyHighlighter(Highlighter): 
    """ 
    Custom highlighter 
    """ 
    def highlight(self, text_block): 
     self.text_block = strip_tags(text_block) 
     highlight_locations = self.find_highlightable_words() 
     start_offset, end_offset = self.find_window(highlight_locations) 
     text_len = len(self.text_block) 

     if text_len <= self.max_length: 
      start_offset = 0 
     elif (text_len - 1 - start_offset) <= self.max_length: 
      end_offset = text_len 
      start_offset = end_offset - self.max_length 

     if start_offset < 0: 
      start_offset = 0 
     return self.render_html(highlight_locations, start_offset, end_offset)