Discussion:
[FreeMarker-user] New built-ins (`?whatever` things)... any opinions/ideas?
Daniel Dekany
2012-08-08 07:18:30 UTC
Permalink
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
Daniel Dekany
2012-08-08 14:01:53 UTC
Permalink
"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.
Post by Daniel Dekany
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
<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
<title>Foo bar${' - '?followed_by(extraTitle)}</title>
${statement?followed_by(';')}
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
Loading...