cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leszek Gawron <lgaw...@mobilebox.pl>
Subject Re: [RT] Attribute Driven Templates (Was: [RT] do me a favor, don't call them taglibs)
Date Sat, 04 Dec 2004 17:58:28 GMT
Daniel Fagerstrom wrote:
> macro(<name>,<param-name>,...,<param-name>)
> -------------------------------------------
> 
> example:
> 
> <table do="macro(mytable,list,td-class)">
>   <tr do="forEach($list)">
>     <td class="${$class}">${item}</td>
>   </tr>
> </table>
> 
> We also need an evalBody as in JXTG. And maybe we should have a 
> possibilty to give a name space to the macro name.
We also need:
- some script compiler directives i.e. for cache control (as in JXTG)
- template inclusion so the templates may be defined in a separate file


Named macros rock! This way we can completely eliminate the need for 
this hack:
http://cocoon.apache.org/2.1/userdocs/flow/jxtemplate.html#eval

Imagine that user wants to render a fancy table providing only a header 
rendering macro and a row template:

<tr do="macro(tableHeader,context)">
   <th>Elem1</th>
   <th>Elem2</th>
</tr>

<tr do="macro(singleRow,context)">
   <td>{context.elem1}</td>
   <td>{context.elem2}</td>
</tr>

<table do="macro(mytable,list,headerMacro,rowMacro)">
   <!-- some fancy longish code here -->
   <tr>
     <th>No.</th>
     <th do="eval($headerMacro)"/>
   </tr>
   <tr do="forEach($list)">
     <td>${index}</td>
     <td do="eval($rowMacro,context=.)"/><!-- problem here -->
   </tr>
</table>

Lovely!

Still I see one problem: as every directive is bound to some tag one 
should be able to eval the macro and strip the root element. In my case 
the output would look like this:

<tr>
   <th>No.</th>
   <tr>
     <th>Elem1</th>
     <th>Elem2</th>
   </tr>
</tr>

This would fix it: <th do="eval($headerMacro,stripRoot=true)"/>

If we used tags we would be able to introduce <jx:template> as in jxtg 
that would not show up in the output. Here we have no such possibility.

> Several directives
> ------------------
> 
> So, how do we handle multiple directives for one element? We could 
> handle the TAL example above like:
> 
> <p 
> do="let(x=/a/long/path/from/the/root;if(x);content(x/txt);attributes(class=x/class)">

IMO this one is not that clear as the same syntax using tags. This will 
be also a lot harder to implement as we will have to create much more 
sophisticated parser. With tags, the xml syntax does 95% of parsing 
itself with only a little amount of additional coding.

> <jx:let name="x" value="/a/long/path/from/the/root">
>   <jx:if test="x">
>     <jx:content select="x/txt">
>       <jx:attribute name="class" value="x/class">
>         <p>Ex Text</p>
>       </jx:attribute>
>     </jx:content>
>   </jx:if>
> <jx:let>
What hits me is how the attribute based syntax can be compact comparing 
to this monster :)

> Formating?
> ----------
> 
> IMO most of the basic formating can be done in a special conversion 
> layer as we have discussed in the convertor thread.
I understand this is completely orthogonal functionality to the one you 
described.

> I have not given any detailed thought to all of the constructions above, 
> just wanted to concretize what attribute driven templating could look 
> like in Cocoon.
> 
> WDYT?
I am only afraid how I will explain the directive ordering to my 
developers. Moreover: how many mistakes they will make, which IMO will 
be much harder to trace comparing to old verbose syntax.

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
Project Manager                                    MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Mime
View raw message