ibexa

Caution: This documentation is for eZ Publish legacy, from version 3.x to 5.x.

shorten

Summary

Returns a shortened version of the input string.

Usage

input|shorten( [ length [, sequence [, trim_type ] ] ] )

Parameters

NameTypeDescriptionRequired
length integer The desired length of the returned string. No.
sequence string Custom trailing/end-sequence. No.
trim_type string Controls the type of trimming: "right" (default) or "middle". No.

Returns

A shortened version of the input string.

Description

This operator shortens the input string to "length" characters and adds a trailing sequence. Please note that the "length" parameter also includes the length of the trailing sequence. If the input string is shorter than "length", it will not be shortened. The default length is 80, the default trailing sequence is three dots: "...". The third parameter controls the type of trimming, it can be set to either "right" (default) or "middle".

Examples

Example 1

{'Led Zeppelin rocks!'|shorten( 15 )}

The following output will be produced: "Led Zeppelin...".

Example 2

{"eZ Systems"|shorten( 7, '...' , 'middle' )}

The following output will be produced: "eZ...ms".

Balazs Halasy (05/02/2004 10:59 am)

Svitlana Shatokhina (06/04/2006 1:46 pm)


Comments

  • Beware of xml :)

    Shorten is a "as is" operator, and will shorten crudely your xml text, counting tags as simple text, and producing seemingly strange output.
    A solution is to use the Php strip_tags operator, as follows :
    {def $xml_content=$node.data_map.champ_xml.data_text}
    {def $text_content=$xml_content|strip_tags()}
    {$text_content|shorten(100)}
    

    In order to be able to use strip_tags, you have to add it to authorized eZ template operators. Edit the template.ini.append.php file of your siteaccess (create it if it does not exist), and add
    [PHP]
    PHPOperatorList[strip_tags]=strip_tags
    


    Forum links :
    http://ez.no/community/forum/setu...g_text_from_summarizing_ezxmltext__1
    http://ez.no/community/forum/deve...e_item_shorten_100_is_not_working__1
  • A more flexible way

    Your solution has a major problem: it destroys all the new lines and screws the layout.

    This extension allows to shorten the xml result, even while keeping all (or a selection of) xml tags.

    http://projects.ez.no/xmlwash

    as an added bonus, it offers a bigger security, no matter your ez config.
  • shorten on a word boundary

    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}