From tapestry-user Mon Dec 17 19:59:22 2007 From: Kaspar Fischer Date: Mon, 17 Dec 2007 19:59:22 +0000 To: tapestry-user Subject: Re: Component with mixed functionality? Message-Id: <9A239AFF-9C41-4E62-A817-1B4349B9D454 () inf ! ethz ! ch> X-MARC-Message: https://marc.info/?l=tapestry-user&m=119792160107392 Thanks, Matt, for your response. On 16.12.2007, at 02:13, Matt Brock wrote: > The problem is that I don't think your even/odd requirement will > work with > any of the regular Tapestry components. Normally you iterate over a > collection of values (the @For component, for example), and use an > OGNL > expression like class="ognl:(index % 2 == 0 ? 'even' : 'odd')" to > determine > whether the row is odd or even. But the problem is, not all of > your rows > are going to be rendered. So what happens when the first row's @If > condition evaluates true, the second row's condition evaluates > false, then > the third evaluates true? You'll end up with two rows both marked > "even". That's exactly what was happening in my case! > The easiest way to do this would be to write your own renderComponent > method. That way you get complete control over the output. This > has the > added advantage of bypassing the OGNL parser. You don't need a > component > definition file (.jwc) or an HTML file, either. Just a single java > class. > Something like this: > > MyComponent.java > > public abstract class MyComponent extends AbstractComponent { > >   @Parameter >   public abstract MyObject getMyObject(); > >   protected void renderComponent(IMarkupWriter writer, > IRequestCycle cycle) { > // You can also use the writer.begin/writer.attribute way of doing > things, > but this is faster. >     StringBuffer output = new StringBuffer(); >     output.append("<table>"); >     int i = 0; >     if (getMyObject().getTitle() != null) { >       output.append("<tr").append > (i % 2 == > 0 ? " class=\"even\"" : > "").append("><td>").append(getMyObject().getTitle()).append > ("</td></tr>"); >       i++; >     } >     if (getMyObject().getNextProperty() != null) { >       output.append("<tr").append > (i % 2 == > 0 ? " class=\"even\"" : > "").append("><td>").append(getMyObject().getNextProperty > ()).append("</td></tr>"); >       i++; >     } >     ...etc... >     output.append(" > "); >     writer.print(output,true); >   } > } I get it. -- I finally took Kristian's suggestion as it allowed me to keep my markup in a HTML-file (even though I don't know whether the latter is worth it). Thanks! Kaspar --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org For additional commands, e-mail: users-help@tapestry.apache.org