You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tiles.apache.org by Tim Kannenberg <tk...@celerasystems.com> on 2008/02/13 19:09:24 UTC

Question re: attribute evaluation

My understanding is that if I specify body content for the <tiles:putAttribute>, that content will be evaluated and the resulting string is what will be available for insertion into my template using <tiles:insertAttribute>.  For example, I have myTemplate.jsp which includes this:

   <table>
      <c:forEach begin="1" end="5" var="current">
         <tr>
            <tiles:insertAttribute name="rowContent"/>
         </tr>
      </c:forEach>
   </table>

and myPage.jsp which includes this:

   <tiles:insertTemplate template="/tiles/myTemplate.jsp">
      <tiles:putAttribute name="rowContent">
         <td><c:out value="${current}"/></td>
      </tiles:putAttribute>
   </tiles:insertTemplate>

Because ${current} is undefined when the body content is evaluated, this produces a table with 5 rows, each of which contains a single cell with nothing in it.  Is this an unavoidable consequence of the way Tiles works, or is there a way to make Tiles treat the content of the putAttribute tag as a literal string to be inserted into the template and then evaluated along with the rest of the template page?
 
Tim Kannenberg
Celera Systems LLC
 


Re: Question re: attribute evaluation

Posted by Antonio Petrelli <an...@gmail.com>.
2008/2/13, Tim Kannenberg <tk...@celerasystems.com>:
> In effect, I'm trying to do
> something analogous to having an abstract search page (the template)
> from which other pages (my individual search pages) can inherit,
> overriding only the pieces which control the rendering of the form
> fields and the result row columns.
> ...
> Is there a different
> approach I can use to achieve the desired results using Tiles?

No, but Struts 2 can help you in building a custom component:
http://www.vitarara.org/cms/struts_2_cookbook/creating_a_ui_component

HTH
Antonio

RE: Question re: attribute evaluation

Posted by Tim Kannenberg <tk...@celerasystems.com>.
Thanks, Antonio.  Unfortunately, I think I chose an example which
doesn't do a very good job of illustrating what I'm actually trying to
accomplish, so let me try to explain more fully.

I'm working on a Struts 2 web application which has several very similar
pages, each of which contains some form fields used to capture search
criteria, along with a table displaying a set of search results which
match the specified criteria.  Each page allows you to search for a
different type of thing (cars, airplanes, boats, etc.), so the set of
search criteria fields differs from one page to the next, as does the
set of columns required to display the results.  However, the basic
layout is consistent from one page to the next, as is some of the markup
for the results table (for example, odd-numbered <tr> elements are
assigned a different class so they can easily be displayed with a
different background color).  I'd like to factor all of this common
stuff (including the Struts <s:form> tags and the <tr> tags) the out
into my template, so that the individual pages which use that template
only have to specify the appropriate set of form fields and the
corresponding set of result row columns.  In effect, I'm trying to do
something analogous to having an abstract search page (the template)
from which other pages (my individual search pages) can inherit,
overriding only the pieces which control the rendering of the form
fields and the result row columns.

I tried to do this by having the individual pages specify the JSP text
needed to render the form fields and the row content using
<tiles:putAttribute>, but it seems like that's not going to work, since
the tag body is evaluated *before* the attribute is substituted into the
template, not after.  From reading about list attributes, I don't think
that mechanism is going to do what I need, either.  Is there a different
approach I can use to achieve the desired results using Tiles?

| -----Original Message-----
| From: Antonio Petrelli [mailto:antonio.petrelli@gmail.com]
| Sent: Wednesday, February 13, 2008 2:00 PM
| To: users@tiles.apache.org
| Subject: Re: Question re: attribute evaluation
| 
| I think I misunderstood your question, sorry.
| I will try to answer better this time.
| 
| 2008/2/13, Tim Kannenberg <tk...@celerasystems.com>:
| > Because ${current} is undefined when the body content is evaluated,
this
| produces a table with 5 rows, each of which contains a single cell
with
| nothing in it.  Is this an unavoidable consequence of the way Tiles
works,
| or is there a way to make Tiles treat the content of the putAttribute
tag
| as a literal string to be inserted into the template and then
evaluated
| along with the rest of the template page?
| 
| It is unavoidable, since the JSP page put inside the
| <tiles:putAttribute> tag body is evaluated *before* putting it as an
| attribute value.
| But in your particular case, why don't you use a list attribute?
| http://tiles.apache.org/tutorial/advanced/list-attributes.html
| 
| Ciao
| Antonio

Re: Question re: attribute evaluation

Posted by Antonio Petrelli <an...@gmail.com>.
I think I misunderstood your question, sorry.
I will try to answer better this time.

2008/2/13, Tim Kannenberg <tk...@celerasystems.com>:
> Because ${current} is undefined when the body content is evaluated, this produces a table with 5 rows, each of which contains a single cell with nothing in it.  Is this an unavoidable consequence of the way Tiles works, or is there a way to make Tiles treat the content of the putAttribute tag as a literal string to be inserted into the template and then evaluated along with the rest of the template page?

It is unavoidable, since the JSP page put inside the
<tiles:putAttribute> tag body is evaluated *before* putting it as an
attribute value.
But in your particular case, why don't you use a list attribute?
http://tiles.apache.org/tutorial/advanced/list-attributes.html

Ciao
Antonio

Re: Question re: attribute evaluation

Posted by Antonio Petrelli <an...@gmail.com>.
2008/2/13, Tim Kannenberg <tk...@celerasystems.com>:
>       <c:forEach begin="1" end="5" var="current">

This way you create a nested scoped variable, so it is visible inside
the <c:forEach> element but *within the same page*.
You could add a <c:set> tag to export this variable into a
request-scoped attribute.

<c:set var="reqCurrent" value="${current}" scope="request" />

HTH
Antonio