Forum Discussion

phoneboy's avatar
phoneboy
Director
6 years ago

How do I retrieve an excerpt of a message with HTML formatting?

We want to retrieve an excerpt of a topic message from a given thread and display it in a custom component.
Currently, we're using something like the following to do it:

<#assign body = (page.context.thread.topicMessage.body)!"" />
<div class="short-message"><@messageBodySimple body, 1500 /></div>

In this example, it pulls out the first 1500 characters.
Unfortunately, it strips all the formatting out of the text, which means it does not display very nice.
Is there a way to do this that preserves the HTML formatting?

  • Looks like this is the way to do it and output the result without escaping the HTML:

    <#assign body = (page.context.thread.topicMessage.body)!"" />
    <#assign truncatedBody = utils.html.truncate(1000, body, "...")>
    ${truncatedBody?no_esc};