在cakephp中突出显示搜索结果

时间:2014-06-04 18:27:19

标签: php cakephp syntax-highlighting highlight cakephp-2.3

正如标题所示,我需要在结果中突出显示搜索的输入字符串,我的意思是如果我查找“ome”,结果应该如下所示:

  1. c ome
  2. s ome
  3. Jer ome
  4. s ome 一个 等
  5. 在文档中搜索我发现我需要激活“Text”帮助器,并且我应该有类似的代码:

    echo $this->Text->highlight($string);
    

    即使我已经尝试了很多可能性,但我无法使其工作,我知道“Text”帮助器正在工作,因为这对我在View / Users / index.ctp中起作用:

    echo $this->Text->autoLinkEmails(h($user['User']['email']));
    

    如果它有帮助,这是我为搜索生成的代码,它正常工作:

    file:Model / Oldcaterm.php

    public $actsAs = array(
        'Search.Searchable'
    );
    
    public $filterArgs =  array(
            'entry' => array(
            'type' => 'like',
            'field' => array('entry', 'lemma_tag')
            ),
            'lemma_tag' => array(
            'type' => 'like',
            'field' => array('entry', 'lemma_tag')
            ),
    );
    

    file:Controller / OldcatermsController.php

    public function index() {
            $this->Oldcaterm->recursive = 0;
            $this->Prg->commonProcess();
            $this->Paginator->settings['conditions'] = $this->Oldcaterm->parseCriteria($this->Prg->parsedParams());
            $this->set('terms', $this->Paginator->paginate());
    }
    

    file:View / Oldcaterms / index.ctp

    <?php
            echo $this->Form->create(null,array());
            echo $this->Form->input('entry', array('label' => 'Cerca coincidència'));
            echo $this->Form->end(__('Submit'));
    ?>
    

    我已阅读并尝试了许多不同的方法,但我无法使其工作,有人请指导我一点,我在文档中找到了下一个例子:

    // called as TextHelper
    echo $this->Text->highlight(
     $lastSentence,
     'using',
     array('format' => '<span class="highlight">\1</span>')
    );
    
    // called as String
    App::uses('String', 'Utility');
    echo String::highlight(
     $lastSentence,
     'using',
     array('format' => '<span class="highlight">\1</span>')
    );
    

    但我找不到让它适合我的代码的方法

    提前致谢。

1 个答案:

答案 0 :(得分:0)

经过研究,我发现 css样式不存在。右键单击浏览器,然后浏览源代码,如果它显示<span class="highlight">Your Text Here</span>,则一切恰到好处。只需将以下代码添加到您的 CSS文件

.highlight{ background-color: #FFDF00; /* <-- Choose what colour you want to use. Here is Golden Yellow */ }

如果仍有问题,请在评论中请分享

相关问题