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

AndyB

Well-known member
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:
There's no way to do this in templates and you'll need to modify the title before it reaches the template.
 
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.
 
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:
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?
 
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.
 
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.
 
You could have used a xen:set:

HTML:
<xen:set var="$searchTerms">{$searchTerm1}|{$searchTerm2}</xen:set>

And it should have worked.
 
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.

Or at least consistent.

Either all bold typeface or not.
 
Back
Top Bottom