Not a developer? Go to MovableType.com

Documentation

trim_to

Trims the input content to the requested length. The entry title “testing the trim_to modifier” might be abbreviated to simply “testing th” using the following example:

<mt:EntryTitle trim_to="10">

As of Movable Type 6.0.4, if trim_to has a negative number value, that number of characters will be trimmed from the end of the output.

Additionally, a value modifier was introduced in the Movable Type 5.1. The trim_to modifier can take an argument in the form “N+STRING” where “N” is a number, “+” is the separator, and “STRING” is a value to be appended to the trimmed text. This can be useful to append an ellipses to a trimmed string, for example:

<mt:EntryTitle trim_to="10+...>

This causes the string “Welcome to Movable Type 5.1!” to be trimmed to to “Welcome to…” — a string of 10 characters followed by three dots.

Of course, this feature is only in Movable Type 5.1 or newer. Older versions need to set and check a variable to do this. Example:

<mt:EntryTitle count_characters="1" setvar="title_length">
<mt:If name="title_length" gt="10">
    <mt:EntryTitle trim_to="10">...
<mt:Else>
    <mt:EntryTitle>
</mt:If>

Example

Emulating 6.0.4 negative value behavior

This code outputs with a trailing comma and space:

<mt:For to="3">Entry Title, </mt:For>

Result:

Entry Title, Entry Title, Entry Title, Entry Title,

In Movable Type 6.0.4, you can do:

<mt:For to="3" trim_to="-2">Entry Title, </mt:For>

Result:

Entry Title, Entry Title, Entry Title, Entry Title

To achieve this in earlier versions of Movable Type, you can use code like this:

<mt:For to="3" setvar="loop_output">Entry Title, </mt:For>
<mt:Var name="loop_output" count_characters="1" setvar="loop_output_length">
<mt:Var name="loop_output_length" op="+" value="-2" setvar="desired_length">
<mt:Var name="loop_output" trim_to="$desired_length">
Back

1 Comment

PRO IT Service

PRO IT Service on February 14, 2011, 9:59 a.m. Reply

Setting this as a variable and then checking against that variable (when the value is greater than a specific value) allows you to control for instance widgets display on busy news sites, where the posts titles vary in length.

Sample:

<mt:EntryTitle countcharacters=”1” setvar=”titlelength”> <div class=”cell<mt:If name=”title_length” gt=”26”> larger</mt:If>”>