You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Kaspar Fischer <fi...@inf.ethz.ch> on 2007/12/11 10:09:49 UTC

Component with mixed functionality?

Hi!

My page needs to output a table with a fixed, predefined number of rows.
Some of these rows may have empty content and should not be rendered.
Each row has a different type (one is a date, another a string, etc.)
and I want a class of "even" or "odd", respectively, be attached to  
<tr>.

My first attempt was

  <table>
   <tr class="even" jwcid="@If" condition="ognl:object.getProperty 
('title').length()>0">
    <td><span key="object-title">Title</span></td>
    <td><span jwcid="@Insert" value="ognl:object.getProperty 
('title')"/></td>
   </tr>
   <!-- ... and a <tr> for each of the additional properties ... -->

The problem is: the <tr> already is a component (@If) but I need to
attach additional behaviour (class should alternate between "even" and
"odd"). What's an easy way to accomplish this?

Thanks,
Kaspar

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Component with mixed functionality?

Posted by Kaspar Fischer <fi...@inf.ethz.ch>.
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 {
>
> &nbsp;&nbsp;@Parameter
> &nbsp;&nbsp;public abstract MyObject getMyObject();
>
> &nbsp;&nbsp;protected void renderComponent(IMarkupWriter writer,
> IRequestCycle cycle) {
> // You can also use the writer.begin/writer.attribute way of doing  
> things,
> but this is faster.
> &nbsp;&nbsp;&nbsp;&nbsp;StringBuffer output = new StringBuffer();
> &nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;table&gt;");
> &nbsp;&nbsp;&nbsp;&nbsp;int i = 0;
> &nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getTitle() != null) {
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append 
> (i % 2 ==
> 0 ? " class=\"even\"" :
> "").append("&gt;&lt;td&gt;").append(getMyObject().getTitle()).append 
> ("&lt;/td&gt;&lt;/tr&gt;");
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
> &nbsp;&nbsp;&nbsp;&nbsp;}
> &nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getNextProperty() != null) {
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append 
> (i % 2 ==
> 0 ? " class=\"even\"" :
> "").append("&gt;&lt;td&gt;").append(getMyObject().getNextProperty 
> ()).append("&lt;/td&gt;&lt;/tr&gt;");
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
> &nbsp;&nbsp;&nbsp;&nbsp;}
> &nbsp;&nbsp;&nbsp;&nbsp;...etc...
> &nbsp;&nbsp;&nbsp;&nbsp;output.append("
> ");
> &nbsp;&nbsp;&nbsp;&nbsp;writer.print(output,true);
> &nbsp;&nbsp;}
> }

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


Re: Component with mixed functionality?

Posted by Matt Brock <br...@gmail.com>.

hbf wrote:
> 
> The problem is: the 
 already is a component (@If) but I need to
> attach additional behaviour (class should alternate between "even" and
> "odd"). What's an easy way to accomplish this?
> 
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".

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 {

&nbsp;&nbsp;@Parameter
&nbsp;&nbsp;public abstract MyObject getMyObject();

&nbsp;&nbsp;protected void renderComponent(IMarkupWriter writer,
IRequestCycle cycle) {
// You can also use the writer.begin/writer.attribute way of doing things,
but this is faster.
&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer output = new StringBuffer(); 
&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;table&gt;"); 
&nbsp;&nbsp;&nbsp;&nbsp;int i = 0;
&nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getTitle() != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append(i % 2 ==
0 ? " class=\"even\"" :
"").append("&gt;&lt;td&gt;").append(getMyObject().getTitle()).append("&lt;/td&gt;&lt;/tr&gt;");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getNextProperty() != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append(i % 2 ==
0 ? " class=\"even\"" :
"").append("&gt;&lt;td&gt;").append(getMyObject().getNextProperty()).append("&lt;/td&gt;&lt;/tr&gt;");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;...etc...
&nbsp;&nbsp;&nbsp;&nbsp;output.append("
");
&nbsp;&nbsp;&nbsp;&nbsp;writer.print(output,true);
&nbsp;&nbsp;}
}

PageThatUsesComponent.html

&lt;span jwcid="@MyComponent" myObject="ognl:somePageObject" /&gt;


-- 
View this message in context: http://www.nabble.com/Component-with-mixed-functionality--tp14270320p14357097.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

Re: Component with mixed functionality?

Posted by Kaspar Fischer <fi...@inf.ethz.ch>.
On 11.12.2007, at 12:30, Kristian Marinkovic wrote:

> have you tried:
> <tr class="ognl:cssClass" jwcid="@If" condition="...
>
> public String getCssClass() {...}

Wow, so simple!

Thanks a lot. I feel almost ashamed: of course, informal
properties can be used and they have OGNL-power, too!

Thanks,
Kaspar

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Component with mixed functionality?

Posted by Kristian Marinkovic <kr...@porsche.co.at>.
have you tried:
<tr class="ognl:cssClass" jwcid="@If" condition="...

public String getCssClass() {...}

informal parameters are allowed and by default the tag will be rendered:
http://tapestry.apache.org/tapestry4.1/components/general/if.html

see also:
http://www.nabble.com/Brain-Freeze-Assistence-Please-to1017361.html#a1017361

g,
kris



Kaspar Fischer <fi...@inf.ethz.ch> 
11.12.2007 10:09
Bitte antworten an
"Tapestry users" <us...@tapestry.apache.org>


An
Tapestry users <us...@tapestry.apache.org>
Kopie

Thema
Component with mixed functionality?







Hi!

My page needs to output a table with a fixed, predefined number of rows.
Some of these rows may have empty content and should not be rendered.
Each row has a different type (one is a date, another a string, etc.)
and I want a class of "even" or "odd", respectively, be attached to 
<tr>.

My first attempt was

  <table>
   <tr class="even" jwcid="@If" condition="ognl:object.getProperty 
('title').length()>0">
    <td><span key="object-title">Title</span></td>
    <td><span jwcid="@Insert" value="ognl:object.getProperty 
('title')"/></td>
   </tr>
   <!-- ... and a <tr> for each of the additional properties ... -->

The problem is: the <tr> already is a component (@If) but I need to
attach additional behaviour (class should alternate between "even" and
"odd"). What's an easy way to accomplish this?

Thanks,
Kaspar

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org