Skip Menu |

This queue is for tickets about the Template-Toolkit CPAN distribution.

Report information
The Basics
Id: 95707
Status: resolved
Priority: 0/
Queue: Template-Toolkit

People
Owner: Nobody in particular
Requestors: stephen.baynes [...] smoothwall.net
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 2.25
Fixed in: (no value)



Subject: truncate filter and HTML special characters
In Manual/Filters.pod it gives the example of truncate(26, '&hellip;') 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.
From: ozcoder [...] gmail.com
Hi, On Fri May 16 06:00:02 2014, https://www.google.com/accounts/o8/id?id=AItOawk8OBCmUaR8VF4IJkvuFJCdAPwqR_w5xEk wrote: Show quoted text
> In Manual/Filters.pod it gives the example of truncate(26, '&hellip;') > 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.
I have created a pull request that goes half way fixing this. See https://github.com/abw/Template2/pull/56 Gordon
Ticket migrated to github as https://github.com/abw/Template2/issues/176