WOOgnl and Inline Bindings

228 views
Skip to first unread message

Mike Schrag

unread,
Mar 4, 2007, 9:10:29 AM3/4/07
to wot...@googlegroups.com
I posted a little mention of this on the "handy things" thread on
wodev, but I thought I'd expound on it here.

If you include the WOOgnl.framework from Project Wonder, it provides
several cool capabilities -- OGNL expressions in bindings, inline
bindings, and helper functions among the big ones. I'll quickly go
over OGNL expressions and then show some of the features of the
inline bindings.

Without doing anything, you get OGNL expression support in bindings.
OGNL (Object Graph Navigation Language) gives you a simple evaluated
language that you can put inside of bindings. If you've ever used
JSP's, this is sort of like EL in that context. To make a binding
evaluate as OGNL, simply put it in quotes and start the binding with
a ~ (tilde). Let's look at an example:

FullName : WOString {
value = "~person.firstName + ' ' + person.lastName";
}

In the above example, we wanted to display the user's full name.
Without OGNL you would have been left making a Java function for what
is effectively a view decision. For instance, what if your designer
decides he wants to show Last Name, First Name? With OGNL that
change can be made in a WOD vs changing code. Kind of cool (when I
wrote about helper functions, I'll show some other ways of looking at
this particular kind of problem). It turns out you can actually do
LOTS of things in OGNL (some dirtier than others :) ). For the full
language definition, take a look at :

http://www.ognl.org/2.6.9/Documentation/html/LanguageGuide/index.html

So that's the 5 second overview of OGNL. With WOOGnl.framework, if
you add the following lines:

ognl.helperFunctions=true
ognl.inlineBindings=true

into your Properties file, you turn on support for helper functions
and inline bindings.

Note that inline bindings is actually an extension of the helper
function parser, so you must have helper functions enabled to have
inline bindings enabled. In the future, these settings may just be
on by default, but for now you have to explicitly enabled them.

Starting from the simplest feature this adds, instead of writing
<webobject name = "Whatever">Something</webobject>, you can now use
the shorter form <wo name = "Whatever">Something</wo>. It's a little
thing, but it's kind of nice.

Helper functions is a little bit more complicated, so I'll just take
on inline bindings for now. WOD/HTML separation purists may be
disgusted by this. If some, avert your eyes till the next post :) I
must say, I was a little skeptical when when I first started working
on it, but there are a couple really handy cases for it.

Inline bindings gives you a JSP-like syntax for WO tags. The syntax is:

<wo:ElementName binding1 = "stringliteral1" binding2 =
"$variable1"/>

For instance, two handy cases for this are WOString and
WOConditional. You can now do:

<wo:WOString value = "$person.name"/>
or
<wo:WOString value = "This is a string literal."/>
or
<wo:WOConditional condition = "$person.isAdmin">I'm an admin!</
wo:WOConditional>

Notice the leading dollar sign in the first case. If you are binding
to a non-string literal like a keypath, or "$true" / "$false" (where
you need the boolean value not the string value), you use the leading
dollar sign.

I personally don't like to use inline syntax for anything that has
more than one or two bindings -- it gets kind of ugly (and the point
is to make things nicer). Currently WOLips also only supports
completion in the old WOD syntax, so you don't get completion.

Well, if we're making things shorter, it's a little tedious that you
have to write WOConditional or WOString. It would be nice if we had
shortcuts for those. Well, you can :) There are a handful of built
in shorcuts -- like "str" for "WOString" and "if" for
"WOConditional", so you could have:

<wo:str value = "$person.name"/>
or
<wo:if condition = "$person.isAdmin">I'm an admin!</wo:if>

Much nicer.

You can also register your own by adding something like the following
to your Application constructor:

WOHelperFunctionHTMLTemplateParser.registerTagShortcut
("WOHyperlink", "link");

One of the cool side-effect features of inline binding syntax is that
you can now define an entire WO/HTML definition with a single
string. This makes it much easier to do things like store WO
templates in a database, or dynamically construct templates. Of
course, Wonder has a component that makes it easy to use inline
binding strings as dynamic component templates thanks to Timo
Hoepfner's ERXInlineTemplate, which might get its own post one of
these days.

ms

Denis Frolov

unread,
Mar 5, 2007, 8:48:04 AM3/5/07
to wotips
Would be really nice to have a working example of using component
definitions from the database, e.g. strings like "Hello, <wo:str value
= "$session.actor.name"/>, ..." that can be edited in e.g. site CMS.

Mike Schrag

unread,
Mar 5, 2007, 9:00:37 AM3/5/07
to wot...@googlegroups.com
If you just want a quick example, you can drop this into a page:

Template : ERXInlineTemplate {
cachingEnabled = false;
html = htmlTemplate;
wod = "";
proxyParent = true;
}

... where "htmlTemplate" is a String containing inline bindings.

ms

Denis Frolov

unread,
Mar 7, 2007, 5:12:04 AM3/7/07
to wotips
Works like a charm! Thanks a lot. Couldn't make it work by myself
without proxyParent binding set to true.
Reply all
Reply to author
Forward
0 new messages