[PHPTAL] nested macro

Laurent Bedubourg lbedubourg at motion-twin.com
Tue Jan 16 15:06:30 CET 2007


Piotr Kabacinski wrote:
 > Hello,
 > I'm wondering if it's possibile to do the following
 >
 > Prepare macro similar to this
 > <div metal:define-macro="main_list" class="list_frame">
 >   <b tal:content="list_title"></b><br/>
 >   <div class="list">
 >      <span tal:replace="list_content"/>
 >   </div>
 > </div>
 >
 > Use it in this way
 > <div metal:use-macro="main_list"
 >   tal:define="list_title string:Main List">
 >   <li>one
 >   <li>two
 > </div>
 >

There's a way to do this without using metal slots :

<div metal:define-macro="main_list">
     <strong tal:content="list_title"/><br/>
     <ul tal:replace="structure list"/>
</div>

<div
metal:use-macro="main_list"
tal:define="list_title string:My title; list">
     <ul>
         <li>One</li>
         <li>Two</li>
     </ul>
</div>

This tal:define defines the 'list_title' and the 'list' content (no 
value is provided thus phptal will use the content of the node instead).

Please note the usage of the tal:replace="structure list", structure 
tells phptal not to escape the value of 'list'.

With metal slots you may use :

<div metal:define-macro="main_list">
     <strong tal:content="list_title"/><br/>
     <ul metal:define-slot="list"/>
</div>

<div
metal:use-macro="main_list"
tal:define="list_title string:my title">
   <ul metal:fill-slot="list">
     <li>One</li>
     <li>Two</li>
   </ul>
</div>


Just a note :

When writing this kind of macro, be sure that it is really usefull, why 
not using CSS classes and meaningful XHTML elements instead of macros ?

In one page :

<div class="mainList">
   <h3>My title<h3>
   <ul>
     <li>One</li>
     <li>Two</li>
   </ul>
</div>

And another page :

<div class="mainList">
   <h3>My other title<h3>
   <ul>
     <li>One foo</li>
     <li>Two bar</li>
   </ul>
</div>

Just customizing the following css items should do the job for every page :

div.mainList {}
div.mainList h3 {}
div.mainList ul {}
div.mainList ul li {}

Less code to maintain and clearer templates... and if one day you want 
to change the way mainList is written (for example adding a footer div), 
you just have to search in your templates the 'class="mainList"' string.

Regards
Laurent
-- 
Laurent Bedubourg
lbedubourg at motion-twin.com
http://www.motion-twin.com



More information about the PHPTAL mailing list