This is a good question (if I understood you correctly!) for it often
confuses people.

A good rule of thumb (and the short answer) is never use
${expressions} in ="attributes", only use them to print out text in
your HTML.

The longer answer involves understanding what's going on; you'll see
you don't really need ${expressions} at all - they exist largely for
your convenience...

If, in your .tml, you want to output a title given by getTitle() in
your java class then you can use the core output component:

public String getTitle() { return "Mr Cool"; }

<t:output value="prop:title"/>

Note the prefix of "prop:", this tells T5 how to interpret the rest of
the string and is called a expression binding. There a number of
default expression bindings provided by T5 and, of course, you can
contribute your own. The default bindings are listed here:

http://tapestry.apache.org/component-parameters.html

By far the most common bindings are 'prop:', 'literal:' and 'asset:'.
In fact,  'prop' is so common it is the default binding, meaning if
you don't supply a binding it is implicitly assumed to be 'prop'.
Therefore you could write:

<t:output value="title"/>

And T5 will know what you mean. But what does 'prop' do?

It is helpful to think of 'prop' as providing a pointer to your getter
/ setter (and you'll see why in a minute), so above we provide a
pointer to the property 'title'. The output component uses the pointer
to 'get' the property and prints it.

So what does ${title} do then? Well, ${expressions} are evaluated,
much in the same way as 'prop' bindings, but the *result* is passed
on, and not the pointer reference. So if we were to do:

<t:output value="${title}"/>

the expression ${title} is evaluated to "Mr Cool" and it is this
string which is passed to the output component, which simply prints it
out.

That worked, but here is where it fails. Lets say we have a list of
Strings called books, a String book property and the following in the
.tml:

<t:loop source="${books}" value="${book}" >
  ...
</t:loop>

${books} would be evaluated and passed to the loop's source parameter.
But when loop wants to set the parameter 'value', ${book} is evaluated
to a (presumably null or empty) String and is passed to loop. But loop
can't set change this String to anything, because it's, well, a final
immutable String! So instead it throws an exception saying 'book' is
read only.

Compare to:

<t:loop source="prop:books" value="prop:book" >
  ...
</t:loop>

Here we're back to passing pointers. So 'loop', again, happily reads
the books source property. But loop tries to set the book value, it
has a pointer to the getter / setter and simply uses it to set a new
value for book. It works and everyone is happy!

If unsure, it is good practice to always explicitly provide bindings
in your attributes - that way you're sure to know what is being
passing around and it should help avoid confusion.

Hope it helps,

Steve.




On 17 September 2011 10:42, Ken in Nashua <[email protected]> wrote:
>
> Hi All,
>
> Under what conditions would I operate the ${} for a property... or not...
>
> Just trying to understand the scope and range of when to de-reference a 
> property.
>
> I look at tml code and sometimes I see properties referenced directly and 
> other times with ${...}
>
> Thanks for any clarification.
> Ken
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to