Subject: | truncate filter and HTML special characters |
In Manual/Filters.pod it gives the example of truncate(26, '…')
This gives the impression that this is a suitable way of truncating a string and inserting an ellipsis character in HTML source. This is misleading or even broken as the truncate filter will think the replacement text is 8 characters long rather than 1 so will remove too many characters if it decides to truncate.
So it would be better to find a less misleading example. Perhaps truncate(26, '[truncated]') would do. But then would also be a good idea to give a correct example of how to handle the HTML ellipsis case as well. Trying to work out this indicates various bits of missing functionality in Template toolkit.
In many cases the correct behaviour will be to use a literal ellipsis and chain truncate and html filters.
[% somevalue | truncate( 10, "…" ) | html %]
The problem here is how to reliably insert the ellipsis character ( "…" ) as template toolkit without using UTF-8 or wide character source as Template toolkit has no way to escape a hex code. Want to be able to do
something like:
[% somevalue | truncate( 10, "\u2026" ) | html %]
Even better would be to be able to use a symbolic name.
Note that truncating something that is already HTML encoded is not a good idea as the truncation may cut into a & encoded character, and will miscount the length anyway if there is any & encoded characters. Maybe an html_truncate filter is required that allows for &encoding of the string and replacement text. (Either <elements> should be regarded as 0 length or their presence treated as an error.)
[It may actually be that HTML truncation is all better done client side in JavaScript or CSS.]
Even if you decide not to provide any additional functionality, please replace your misleading/broken example.