Re: length-safe substring ?
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Monday, March 8, 2010, 9:55:25 PM, Grégory Joseph wrote:
> Hi,
>
> When trying to display the first N characters of a string, not
> knowing the size of said string, in my template, I'm doing this:
> [#assign foo='a fairly short string']
>
> ${foo?right_pad(50)?substring(0, 50)?trim}
>
> This seems rather cumbersome, wouldn't you agree? Another option
> would be to use if blocks; not much more readable.
>
> So what I'm asking:
> - is there a simpler/shorter way of doing the same ?
No... Except of course defining a #function for it. There you could
use ?length to find out if the string is too long instead of the
?right_pad hack.
> - if not, would a new built-in make sense ? Or changing the behavior
> of the existing one to be more lenient than its java.lang.String
> equivalent ?
There definitely should be, it's a frequently needed thing on Web
pages. However, it doesn't really have a standard way. Some will just
truncate the string, but I for example prefer to end with a "..." or
"[...]", so it's obvious that the text was truncated. And then there
the "..." is possibly three dots, or a UNICODE ellipsis... (Oh, and
some colorize it to gray, which simply can't be done with built-in as
the HTML tags will be escaped.) Anyway... I think something like
?limit(50) and s?limit(50, "[...]") would be useful, as it covers most
needs.
> Cheers,
>
> -g
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev