• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Using foreach in a template, how to bold some words in thread title

AndyB

Well-known member
#1
I have the following code in a template:

Code:
        <xen:foreach loop="$threads" key="$index" value="$thread">
   
        <tr class="dataRow">
        <td><a href="{xen:link threads, $thread}" title="{$thread.nodetitle}" target="_blank">{$thread.nodetitle}</a></td>
        <td><a href="{xen:link threads, $thread}" title="{$thread.title}" target="_blank">{$thread.title}</a></td>
        </tr>
   
        </xen:foreach>
Currently the thread title (as a link) would show something like this:

"This is an example thread title"

what I would like to do is bold two words like this:

"This is an example thread title"

How can I accomplish this? The words to be bolded would be in the variables $searchWord1 and $searchWord2.

Thank you.
 
Last edited:

Jeremy

Well-known member
#3
You may be able to use this (or a variation), but its untested:
Code:
{xen:helper snippet, $page.content, 150, {xen:array 'term={$search.search_query}', 'emClass=highlight', 'stripHtml=1'}}
Although, that'd reduce the title length if set too small.
 

AndyB

Well-known member
#4
That works for one search word. Here's what I have so far.

Code:
        <xen:foreach loop="$threads" key="$index" value="$thread">
     
        <tr class="dataRow">
        <td><a href="{xen:link threads, $thread}" title="{$thread.nodetitle}" target="_blank">{$thread.nodetitle}</a></td>
     
        <td><a href="{xen:link threads, $thread}" title="{$thread.title}" target="_blank">{xen:helper snippet, $thread.title, 150, {xen:array 'term={$searchWord1}', 'emClass=highlight', 'stripHtml=1'}}</a></td>
     
        </tr>
     
        </xen:foreach>
Now to figure out how to bold the second word.
 
Last edited:

AndyB

Well-known member
#5
If I change:

'term={$searchWord1}'

to

'term={$searchWord2}'

then my second search word is bolded.

I wonder if there is a way to have both search words used in the term?
 

AndyB

Well-known member
#6
Looking at the source code here:

XenForo/Helper/String.php

PHP:
	public static function highlightSearchTerm($string, $term, $emClass = 'highlight')
	{
		$term = strval($term);
		if ($term !== '')
		{
			return preg_replace('/(' . preg_quote($term, '/') . ')/si', '<em class="' . $emClass . '">\1</em>', htmlspecialchars($string));
		}

		return htmlspecialchars($string);
	}
It looks like this function is designed to return only one highlighted word.
 

AndyB

Well-known member
#9
After seeing how the thread title links look with some words bolded, I think I will abandon this idea. Thread title links are much easier to read when there are no bolded words.
 

Jeremy

Well-known member
#10
You could have used a xen:set:

HTML:
<xen:set var="$searchTerms">{$searchTerm1}|{$searchTerm2}</xen:set>
And it should have worked.