2016-12-29 18 views
1

私はWordpress用のTimberプラグインを使用しています。Twigハイライトワード(Timber Plugin)

結果検索ページを作成します。ユーザーが検索した単語を強調したいと思います。 PHPで

私がいることを書いた:

<p class="date red"><?php echo str_ireplace($terms, $highlight, get_field('subtitle_post')); ?></p 

しかし、私は小枝(木材)でそれを変換する方法がわからない:PHPでの検索単語を置き換えるために

$highlight = array(); 
if ($terms) { 
    foreach($terms as $term) { 
     array_push($highlight, '<span class="blue bold">'.$term.'</span>'); 
    } 
} 

そして、その、 ?

答えて

1

カスタムの小枝フィルターを使用する必要があります。

文書から:extending timber。そして、あなたは

{{ yourField|highlight(words) }} 
を使用してカスタムフィルタを使用することができ

/* functions.php */ 

add_filter('get_twig', 'add_to_twig'); 

function add_to_twig($twig) { 
    /* this is where you can add your own fuctions to twig */ 
    $twig->addExtension(new Twig_Extension_StringLoader()); 
    $twig->addFilter(new Twig_SimpleFilter('highlight', 'highlight')); 
    return $twig; 
} 

function highlight($text, array $terms) { 

    $highlight = array(); 
    foreach($terms as $term) { 
     $highlight[]= '<span class="blue bold">'.$term.'</span>'; 
    } 

    return str_ireplace($terms, $highlight, $text); 
} 

(私はあなたの例に適応させることを試みたが、あなたはそれを変更する必要がある場合があります)