[xwiki-users] $xwiki not always available?

Erin Schnabel ebullient.rain at gmail.com
Mon Jul 30 19:40:31 CEST 2007


On 7/30/07, Thomas Drevon <thomas.drevon at intermedia.uio.no> wrote:

[snip..]

>
> Thanks for taking the time to answer me! I see the flaw that you
> pointing out.
>
> > The recommended (from the velocity docs) way to check for non-null && not empty:
> >
> > #if( "$!allTags" != '')
> >   #set( $allTagsSorted = $xwiki.sort($allTags))
> > #end
> >
> > That should solve the problem.
>
> I ended up doing something similar:
>
> #set( $allTags = $xwiki.search($query))
> #if ( $allTags.size() > 1)
>    ...do stuff
> #end
>
> However, I'm absolutely positive that my list of tags isn't empty, so
> this can't the ultimate cause of the problem. This was confirmed by
> Sergiu Dumitriu; he recently updated the jira issue concerning the
> inclusion of the above mentioned tag cloud in xwiki. Check it out:
> http://jira.xwiki.org/jira/browse/XE-51
>

Well, I've observed some interesting behavior with what you've just
mentioned. For example, if any other place in your script also uses
$allTags, and $xwiki.search($query) barfs for some reason, the
other/dirty value will stay in $allTags. Also, if $allTags is empty,
$allTags.size() will be the text $allTags.size(), and the log will
gripe that you can't compare that to be > 1.

The likelihood of that, given that your list of tags isn't empty makes
all of the above unlikely of course, but it's good to bear in mind
that weird behavior that is almost impossible to figure out can happen
when variables aren't checked for empty. The crappy part about that is
using "$var" can end up rendering the content... not a perfect
solution by any means, but one that is the safest if you want non-null
and not empty.

#set($allTags = '')
#set( $allTags = $xwiki.search($query))
#if( "$!allTags" != '' && $allTags.size() > 1)
    ...do stuff
#end

that should work every time. ;)

-- 
'Waste of a good apple' -Samwise Gamgee




More information about the users mailing list