Re: help:missing value test (node??) is not working
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Thursday, June 9, 2011, 1:04:42 PM, krist0ph3r wrote:
> I've found that checking for a node's existence doesn't work unless you check
> for the 1st element in that node. In your example:
>
> <#if mouse[0]??>
> Mouse found
> <#else>
> No mouse found
> </#if>
>
> however, if mouse is a variable and not a node (and you're checking if it is
> defined, not if it exists), you can just use mouse?? as you have in your
> question.
Yeah, that's something almost everyone runs into. This is documented
at: http://freemarker.org/docs/xgui_imperative_learn.html
The cause is that when you issue someElement.mouse, you get the *list*
of matching nodes (just like with XPath). It can be many nodes, only
one node, or 0 nodes. In all of these case the result list itself
exists, only it's possibly empty. If someElement.mouse would return
nothing instead of a 0-length list, someElement.mouse?? would work,
but then
<#list someElement.mouse as m>
... do something with each m
</#list>
would fail, which would be again rather unpractical. That said, I
don't know how to solve this properly (I mean, like in a newer XML
wrapper or with language changes). For example, ?? and ! could treat
empty collections as non-existant... but that would be a mess if we
aren't talking about XML queries like this. Or there should be
NonExistantTemplateModel interface with which a value can suggest that
if ?? or ! asks, it doesn't exist... rather dirty hack and maybe it
would have unforeseen consequences.
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev