I have added the str?truncate(maxLength) built-in (and its other
variations) to the 2.3-gae head. This is something that users have
requested and implemented their own for who knows how many times, yet
it wasn't added to FreeMarker till now, as there's no single correct
way of text truncation. So what I did is making the algorithm
pluggable, and giving a default that I think reflects a good practice.
I guess most users will just go along (instead of quickly adding some
simplistic #function their own), but those who have other ideas can
still keep using ?truncate.

Please tell your opinions, ideas, or even better, test it!

From https://freemarker.apache.org/builds/fm2/versions_2_3_29.html:
Added new built-ins for truncating text. string?truncate(length)
truncates the text to the given length, and by default adds [...] at
the end if truncation has happened. Truncation happens at word
boundaries, unless the result is too short that way, in which case it
falls back to truncation mid word. There's also ?truncate_w to force
Word Boundary truncation, and ?truncate_c (for Character Boundary)
that doesn't care about word boundaries. The truncation algorithm is
pluggable in the FreeMarker configuration. See the reference for more
details.

Built-in documentation:
https://freemarker.apache.org/builds/fm2/ref_builtins_string.html#ref_builtin_truncate

Related API-s:
https://freemarker.apache.org/builds/fm2/api/freemarker/core/TruncateBuiltinAlgorithm.html
https://freemarker.apache.org/builds/fm2/api/freemarker/core/DefaultTruncateBuiltinAlgorithm.html
https://freemarker.apache.org/builds/fm2/api/freemarker/core/Configurable.html#setTruncateBuiltinAlgorithm-freemarker.core.TruncateBuiltinAlgorithm-

Commit:
https://github.com/apache/freemarker/commit/7c5ef10ef3da3b94fc5cdf9d61c966282b6cd8ac

-- 
Thanks,
 Daniel Dekany

Reply via email to