Highlight search terms in search results

Apologies if this has been asked before - couldn't find it. The design I am trying to implement wraps a span around instances of the search string in the search results. I couldn't see a way of doing this out of the box, and my first stab at keyword modifiers doesn't seem to do it:

%asset_name_linked^replace_keywords:replace:{globals_get_queries_term_query}:<span 
class="searchTerm">{globals_get_queries_term_query}</span>%

Can anyone sort out my modifiers, or give another way of doing this?

 

Edit: just realized that of course I would not want to modify %asset_name_linked% as it would destroy the link HTML, so ignore my choice of base keyword replacement - it's the modifiers that I need to get right.

 

Thanks

 

Tom

Are you opposed to using javascript? It would be super simple doing it that way. Sometimes those long keyword modifier solutions seem like too much work.

+1 for using JS, doing this server side seems like it would decrease performance for very little benefit.

If you really do want to do it serverside, maybe the tag or tagif modifiers? 

 

tag:<tagname> Wraps a returned value in a set tagname argument XML tag.
Example: %asset_contents^tag:body%
Effect: <body>content</body>

 

tagif:<tagname> Wraps a returned value in a set tagname argument XML tag. If the returned value is empty, no tags are printed.

Example: %asset_name^tagif:title%
Effect: <title>assetname</title>

(http://manuals.matrix.squizsuite.net/keyword-replacements/chapters/keyword-modifiers)

 

You'd still need a replace keyword to get the class into the tag though, I imagine. (maybe something like replace^<span:<span class="searchTerm"% will do)

Here is a quick JS version: http://jsfiddle.net/w5ze6/119/

Are you opposed to using javascript?

 

For something as simple as this, yes. Why on earth would I want to deny a proportion of my users a feature which the client-server model does not dictate can only happen client-side?

 

maybe the tag or tagif modifiers

 

I've used those extensively before. Problem is that the design requires the surrounding tag to have a class. I don't suppose tagif supports them, does it? Something like:

 

%asset_name_linked^tagif:span.searchTerm%

 

I've used those extensively before. Problem is that the design requires the surrounding tag to have a class. I don't suppose tagif supports them, does it? Something like:

%asset_name_linked^tagif:span.searchTerm%

 

Can you get away with highlighting it without using a class? Can't you just wrap it in a <strong> tag or target it via parent CSS selector?

 

p.result-title > strong {

   highlight: stuff

}  

 

?

I've used those extensively before. Problem is that the design requires the surrounding tag to have a class. I don't suppose tagif supports them, does it? Something like:

 

 

Hi Tom,

 

I've just tested the example I gave in my last reply and it works for me.

 

%asset_name^tagif:span^replace:<span:<span class="x"%

 

Chris

Can't you just wrap it in a <strong> tag or target it via parent CSS selector?

 

I don't know that that would be quite semantically correct, though perhaps the q tag (if it's not deprecated) would be appropriate. Thanks. I still don't really understand why that might work when my version does not, though.

 

%asset_name^tagif:span^replace:<span:<span class="x"%

 

But that wraps the whole string, not just instances of the search query within it. Or am I misunderstanding?

 

Thanks

 

Tom

Well, I just can't get replace_keywords to work at all in this context, which rings a bell - did I post elsewhere about it not working in another context? I'm on 4.14.3.

Hi Tom,

 

Apologies - I had misunderstood the problem.

 

This works for me:

%asset_name^replace_keywords:replace:({globals_get_query}):<span class="searchTerm" ><strong>{globals_get_query}</strong></span>%

 

I haven't seen 'globals_get_queries_term_query' is that just for a GET parameter called 'queries_term_query'?

 

This is in 4.18.3 however, and I think early versions of the 'replace_keywords' modifier did not support globals keywords in the replacements... I'm not sure what version that changed in, but I would try your modifier using a meta field or short_name attribute for the search term, just to confirm whether it's a problem with the globals keywords inside a replace_keywords modifier. 

Hi Chris

 

No, yours doesn't work for me, and I've confirmed that if I try to highlight a non-global keyword, it works. Yes, that is my search query GET variable, using the replacement specified for search results bodycopies:

%<field_name>_query_terms%

I've copied the search field code from an existing site on our Matrix install. Is the rather unwieldy search field name hard-coded, or can I set it somewhere. I have concerns that there is a naming conflict between the keyword and the modifier.

 

We're on 4.14.3. Can anyone confirm that modifiers don't work on global keywords in this version?

 

Thanks

 

Tom