Skip to content

View Helpers

highlight()

highlight() — returns string

Available in: controller Category: Miscellaneous Functions

Searches the given text for one or more phrases and wraps all matches in an HTML tag (default: <span>). This is useful for search results or emphasizing certain keywords dynamically.

NameTypeRequiredDefaultDescription
textstringyesText to search in.
phrasestringnoPhrase (or list of phrases) to highlight. This argument is also aliased as phrases.
delimiterstringno,Delimiter to use when passing in multiple phrases.
tagstringnospanHTML tag to use to wrap the highlighted phrase(s).
classstringnohighlightClass to use in the tags wrapping highlighted phrase(s).
encodebooleannotrueEncode URL parameters using EncodeForURL(). Please note that this does not make the string safe for placement in HTML attributes, for that you need to wrap the result in EncodeForHtmlAttribute() or use linkTo(), startFormTag() etc instead.
1. Basic usage (default <span class="highlight">)
#highlight(text="You searched for: Wheels", phrases="Wheels")#

// Output:
// You searched for: <span class="highlight">Wheels</span>

2. Highlight multiple phrases
#highlight(
    text="ColdFusion and Wheels make development fun.",
    phrases="ColdFusion,Wheels"
)#

// Output:
// <span class="highlight">ColdFusion</span> and <span class="highlight">Wheels</span> make development fun.

3. Use a custom delimiter for multiple phrases
#highlight(
    text="Apples | Oranges | Bananas",
    phrases="Apples|Bananas",
    delimiter="|"
)#

// Output:
// <span class="highlight">Apples</span> | Oranges | <span class="highlight">Bananas</span>

4. Use a different HTML tag
#highlight(
    text="Important: Read the documentation carefully.",
    phrases="Important",
    tag="strong"
)#

// Output:
// <strong class="highlight">Important</strong>: Read the documentation carefully.