GRolf | 21 Oct 2011 21:59

nested UL


I have a multilevel array, where, as the template concerns, the number 
of levels is unlimited.

eg
$objects = array('Food'=>array('Fruit'=>array('Red'=>array('Cherry', 
'Strawberry'), 'Yellow'=>array('Banana')), 'Meat'=>array('Steak', 
'Hamburger'));

I'd like to loop through this array in my templates and end up with

<ul>
<li>Food
   <ul><li>Fruit
     <ul><li>Red
       <ul><li>Cherre</li><li>Strawberry</li></ul>
     </li><li>Yellow
... etc

How could I do this, without needing to know the number of levels?

--

-- 

GRolf
Marco Pivetta | 21 Oct 2011 22:09
Picon
Gravatar

Re: nested UL

What about a recursive macro?

<ul metal:define-macro="somelist" tal:condition="php:count(elements)">
    <li tal:repeat="element elements">
        <tal:block tal:condition="php:is_array(element)">
            <tal:block tal:define="elements element"/>
        </tal:block>
        <tal:block tal:condition="not:php:is_array(element)" tal:content="element"/>
    </li>
</ul>

<tal:block tal:define="elements fruits" metal:use-macro="somelist"/>

Marco Pivetta
http://twitter.com/Ocramius
http://marco-pivetta.com



On 21 October 2011 21:59, GRolf <gerolf <at> pictureparking.com> wrote:

I have a multilevel array, where, as the template concerns, the number of levels is unlimited.

eg
$objects = array('Food'=>array('Fruit'=>array('Red'=>array('Cherry', 'Strawberry'), 'Yellow'=>array('Banana')), 'Meat'=>array('Steak', 'Hamburger'));

I'd like to loop through this array in my templates and end up with

<ul>
<li>Food
 <ul><li>Fruit
   <ul><li>Red
     <ul><li>Cherre</li><li>Strawberry</li></ul>
   </li><li>Yellow
... etc


How could I do this, without needing to know the number of levels?


--

GRolf

_______________________________________________
PHPTAL mailing list
PHPTAL <at> lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

_______________________________________________
PHPTAL mailing list
PHPTAL@...
http://lists.motion-twin.com/mailman/listinfo/phptal

Gmane