php - Highlight searched word -
in symfony2, how highlight word search search box :
// search twig:
{% block body -%} <h1>results of "{{ find }}"</h1> {% if entities%} {% entity in entities %} <table class="record_properties"> <tbody> <tr> <td>><a href="{{ path('onequestion_show', { 'id': entity.id }) }}">{{ entity.question }}</a></td> </tr> </tbody> </table> {% endfor %} {%else%} <td>no results found</td> {%endif%} {% endblock %}
//searchcontroller :
public function searchaction() { $request = $this->getrequest(); $data = $request->request->all(); $find = $data['search']; $em = $this->getdoctrine()->getmanager(); $query = $em->createquery( 'select p.id, p.question epitaepitabundle:questionanswer p p.question :data') ->setparameter('data', "%$find%"); $res = $query->getresult(); return $this->render('epitaepitabundle:questionanswer:search.html.twig', array( 'entities' => $res, 'find' => $find)); }
// getting searched result want highlighted...
you could, probably, like:
<td>><a href="{{ path('onequestion_show', { 'id': entity.id }) }}">{{ entity.question|replace({find: "<span class='highlight'>" ~ find ~ "</span>"}) }}</a></td>
and create class in css:
span.hightlight { /* whatever want highlighted elements*/ }
another option write own filter in php.
Comments
Post a Comment