View Helpers
highlight()
Signature
Section titled “Signature”highlight() — returns string
Available in: controller
Category: Miscellaneous Functions
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text | string | yes | — | Text to search in. |
phrase | string | no | — | Phrase (or list of phrases) to highlight. This argument is also aliased as phrases. |
delimiter | string | no | , | Delimiter to use when passing in multiple phrases. |
tag | string | no | span | HTML tag to use to wrap the highlighted phrase(s). |
class | string | no | highlight | Class to use in the tags wrapping highlighted phrase(s). |
encode | boolean | no | true | Encode 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. |
Examples
Section titled “Examples”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.