Skip to the content
Documentation

shorten on a word boundary

No ratings yet

Code snippet to break on a word boundary.
Basic strategy is to truncate the string, then add characters until you encounter a space, or run out of string.

{def $sub=$node.object.data_map.short_description.content.xml_data
        $subsub=''
        $len=count($sub)
        $limit=598
        $chr=''
        $i=0}
{if ge($len,$limit)}
        {set $i=$limit|sub(50)}
        {set $subsub=$sub|shorten($i,'')}
        {set $chr=$sub|extract($i,1)}
        {while and(ne($chr,' '),lt($i,$limit))}
                {set $subsub=$subsub|concat($chr)}
                {set $i=$i|inc}
                {set $chr=$sub|extract($i,1)}
        {/while}
        {set $subsub=$subsub|concat(' ...')}
{/if}