1. 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

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 18, 2013.

  1. AndyB

    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: Oct 18, 2013
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    There's no way to do this in templates and you'll need to modify the title before it reaches the template.
     
    AndyB likes this.
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  4. AndyB

    AndyB Well-Known Member

    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: Oct 18, 2013
  5. AndyB

    AndyB Well-Known Member

    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?
     
  6. AndyB

    AndyB Well-Known Member

    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.
     
  7. Jeremy

    Jeremy XenForo Moderator Staff Member

    $searchTerm1|$searchTerm2
     
  8. AndyB

    AndyB Well-Known Member

    Thank you for the suggestion, Jeremy. Unfortunately the template editor recognizes that as invalid.
     
  9. AndyB

    AndyB Well-Known Member

    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.
     
  10. Jeremy

    Jeremy XenForo Moderator Staff Member

    You could have used a xen:set:

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

    Amaury Well-Known Member

    Or at least consistent.

    Either all bold typeface or not.
     

Share This Page