Re: [Freemarker-devel] New built-ins (`?whatever` things)... any opinions/ideas?
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user,gmane.comp.web.freemarker.devel |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, August 8, 2012, 9:52:13 AM, Denis Bredelet wrote: > "chose" should really be "either" I think. (a < b)?either(a, b) (a < b)?chose(a, b) I don't know... any votes? Or an even better name? > Not sure what's the use of ?char Character-code arithmetic is frequently needed for converting 1, 2, 3, etc. to 'A', 'B', 'C', etc. However, now that you ask, I can't image any other use-cases, so I should address that directly. n?number_as_letter n?number_as_lower_letter which would return 'A'/'a' for 1, 'Z'/'z' for 26, 'AA'/'aa' for 27, 'AB'/'ab' for 28, etc. Speaking of which, maybe ?roman should be added too, which converts 1 to "I", 2 to "II", etc. > or ?bin_8. Actually I only know that converting to hexadecimal is frequently requested. Now that I think about it, the binary-related requests was about testing if a bit is set. So then maybe we only need: n?[j_]hex_[8|16|32|64] and n?bit(index) which returns a boolean. > I like followed_by and dump > > Should min and max work on lists instead of single values? They should have vararg parameter list. Also, they could work on sequences too. Although the I guess most use-cases are with two numbers. -- Best regards, Daniel Dekany > -- Denis. > > On 8 Aug 2012, at 08:18, Daniel Dekany <[email protected]> wrote: > >> I'm planning to add some new buildins to FreeMarker 2.3.x. I'm not >> sure which of these will go to the next release, but certainly not all >> of them (lack of time...). I would like to hear some opinions, or >> ideas regarding better names and semantics. Also regarding what other >> built-ins you miss. >> >> boolExp?chose(whenTrue, whenFalse) >> >> This one is instead of Java's ternary operator (exp ? exp : exp), that >> FTL can't have due to syntactical reasons. Of course, the operators >> are evaluated on demand. It's like #if/#else, but it's an expression. >> >> n?until(m[, step=1]) >> n?to(m[, step=1]) >> These would deprecate the ".." operator, which was sadly >> mis-designed back then. They create a numerical range, always with >> step 1 (not -1) unless `step` was provided. "until" has exclusive end, >> "to" has inclusive end. >> >> s?safe_substring(from[, to]) >> >> Same as ?substring, but ignores out-of-ranges >> >> n?char >> >> Converts UCS codepoint to character. Like 65?char is 'A'. >> >> n?abs >> Absolute value >> >> n?[bin|[j]hex|oct]_[8|16|32|64] >> That is ?bin_8, ?bin_16, ..., hex_8, hex_16. The difference between >> "hex" and "jhex" is tat the last prints with a 0x prefix. >> >> n?is_nan, n?is_infinity >> Works only for numbers, tells if the wrapped number is float or >> double NaN or Infinitiy (positive or negative). >> >> exp1?followed_by(exp2) >> >> This returns exp1 + exp2, but only if both are defined and are >> non-0-length strings. This is useful in situations like these: >> >> <title>Foo bar<#if extraTitle??> - ${extraTitle}</#if></title> >> >> or >> >> <#if statement??>${statement};</#if> >> >> That is, you want to print an optional variable and when it exists, >> you also want to print something before or after it. These would >> become to: >> >> <title>Foo bar${' - '?followed_by(extraTitle)}</title> >> >> ${statement?followed_by(';')} >> >> min/max: >> >> I wonder how to add min/max functions. `n?min(m)` looks silly IMO. >> It could be `.min(n, m)` instead, where the initial dot is for the >> special-variable syntax (used for .data_model, .version and like). >> Only it's something most user don't know... >> >> exp?dump >> exp?dump_as_text >> >> Prints the value for debugging purposes. ?dump would print it as >> XHTML, while dump_as_text as text/plain. >> >> -- >> Best regards, >> Daniel Dekany ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/