Re: How does the ?string for boolean work
Attila Szegedi <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
On 2008.02.28., at 20:57, Newman, John W wrote:
> Hi,
>
> Is the ?string built in like the ternary operator? I mean, does it
> evaluate the expression first and then process just one side, or
> does it process both sides first? I’m thinking it is the latter,
> and it should work the other way if possible.
Technically, ?string on a boolean is a TemplateMethodModelEx, and yes,
all of its arguments are evaluated prior to invocation. We introduced
lazy evaluation of method argument expressions for 2.4, so in 2.4 it
will work the way you expect it will (evaluating exactly one of its
argument expressions). We discussed backporting this behaviour to 2.3,
but it - even if admittedly as a corner case - could break backwards
compatibility, so we decided against it.
Under the hood, this works as follows: in 2.4, any
TemplateMethodModel(Ex) that also implements the marker interface
"LazilyEvaluatableArguments" will receive the "raw"
freemarker.core.ast.Expression objects on invocation; the engine will
not evaluate them automatically to a TemplateModel as it does for
default "eager" method models. The method class' implementation is
then responsible for evaluating the expressions on its own as it needs
them.
>
> Here’s what I’m doing, I feel that it should work but it doesn’t.
>
> [#list 0..options?size - 1 as i]
> [#assign current = options[i] /]
> <div id="options.${current}.prev" class="hidden">${(i == 0)?
> string('', options[i - 1])}</div>
> <div id="options.${current}.next" class="hidden">${(i ==
> options?size - 1)?string('', options[i + 1])}</div>
> [/#list]
>
> The processing fails at evaluating options[i – 1] the first time,
> even though i == 0 and it should just dump out ‘’. So can that
> built in evaluate the expression before both the values? Does it and
> I’m doing something wrong? Thoughts?
>
> The context of what I’m doing here is pretty out there, and if the
> IE team didn’t take 4 years to fix a bug with their select widget I
> wouldn’t be in this situation. All I need to do is hide options in
> a select widget…. They have known about this since 2004, rewrote the
> select widget to not use the shell32 one for IE7, but didn’t add
> that functionality to the new widget (and lot of other standards
> compliant stuff was skipped). We won’t see that until IE8 which is
> at least 2011 … that’s right it is going to take them 7+ years to do
> that!
>
> I asked you guys about varargs support and within 2 weeks it was
> done and released! Yet the ‘best’ software company we have takes 7
> years to support standards! crazy world…
:-)
Attila.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/