WOOgnl and inline OGNL

30 views
Skip to first unread message

eric robinson

unread,
Mar 6, 2007, 10:34:29 AM3/6/07
to wotips
Building on a previous post from Mike, consider an inline
WOConditional,

<wo:if condition = "$person.isAdmin" negate = "$true">

which would evaluate to true if the person is not an admin. But let's
be honest that [negate = "$true"] is kind of ugly.

Here we can use inline OGNL in the condition statement. If we start
the binding value with a ~ instead of a $, we can insert some OGNL
code will be evaluated. So, our not admin condition becomes...

<wo:if condition = "~ !person.isAdmin">

Much better!

We can build on this idea a little and evaluate some simple
expressions with OGNL. Say that we want to display a blank state
message on a blog posts list component if there are no blog posts yet.
Using inline bindings, our first pass might look something like this.

<wo:if condition = "$noBlogPosts">

which would point to a method in the component's java file.

Using OGNL we can get rid of the need for that extra method...

<wo:if condition = "~ blogPosts.count == 0">

I can hear the screams now ("but you're putting logic into your
template!"), but I find that for simple cases such as this, its
actually much clearer and more direct to do it this way. In addition,
it makes your source files cleaner because they don't have wrapper
methods for every conceivable condition that shows up in your HTML
templates.

A couple pitfalls to watch out for. The parser for HTML templates
doesn't like either < or >, I can never remember which, inside of
inlinen OGNL very much, so I generally try to avoid using either of
them. Also it's important to remain DRY, just because you can do
things inline doesn't mean that its better, if you're building a
complicated string (such as a javascript callback or DOM id), it's
probably better to move that logic out into a class or helper method,
especially if that string is used in more than one place. I've never
really tried, but I would advise against trying to do anything
equivalent to a "set" method inside of inline OGNL, that just seems
nasty.

This technique may be giving some people just enough rope to hang
themselves with. But, in appropriate context, inline OGNL can be very
useful and a real timesaver.

Reply all
Reply to author
Forward
0 new messages