Skip to content

Variable selectors in loops and nested iterations for a multi level menu #200

@SolidSnakeGr

Description

@SolidSnakeGr

Hello there, I'm trying to achieve a multi level menu using the following structure:

$data = array('menu_items' => array(
1 => array('id' => 1, 'title' => 'Home', 'parent_id' => 0),
2 => array('id' => 2, 'title' => 'Pages', 'parent_id' => 0, 'children' => array(

1 => array('id' => 3, 'title' => 'First', 'parent_id' => 2),
2 => array('id' => 4, 'title' => 'Second', 'parent_id' => 2)
))));

My layout has an element like this where our menu items should be populated in:
<ul class="menu-items"></ul>

We use the following template (menu_item.xml):

<?xml version="1.0"?>
<template>
<li><span>test title</span><ul class="submenu-items"><li></li></ul></li>
</template>

And our .tss goes like this:

.menu-items {content: template("menu_item.xml")}
.menu-items > li {repeat: data(menu_items)}
.menu-items > li:attr(class) {content: "item-",iteration(id)}
.menu-items > li > span {content: iteration(title)}
.menu-items > li > .submenu-items:iteration[children!=true] {display:none}

.menu-items > li > .submenu-items > li:iteration[children=true] {repeat: iteration(children)}
.menu-items > li > .submenu-items > li:iteration[parent_id!=0] {content: template("menu_item.xml"); content-mode: replace}
.menu-items > li > .submenu-items > li > span {content: iteration(title)}

Now the resulting html is the following:

<ul class="menu-items">
<li class="item-1"><span>Home</span></li>
<li class="item-2"><span>Pages</span><ul class="submenu-items">
<li><span>Pages</span><ul class="submenu-items"><li></li></ul></li>
<li><span>Pages</span><ul class="submenu-items"><li></li></ul></li>
</ul></li>
</ul>

So, my first question is, how can we now select the "item-2" class in order to make sure its children will only populate its own "ul.submenu-items"?

Being able to use an iteration value as a variable to the selector would be great IMHO.

And my second question is do nested iterations work?

In the example above I'm trying to run a loop within a loop, but the top level iteration's values override the lower level iterations, when the key names are the same, which is common when working with similar objects/arrays in a tree form.

So, what I would love to be able to do is use something like the following tss command:

.menu-items > li.item-:iteration[parent_id] > .submenu-items > li > span {content: iteration(title)}

..or something similar, maybe have a value[] command that can be called to use the value in the selector.
Either way, thanks for this amazing tool, transphporm is my favorite way of populating data in a view.

Edit: Tried to prevent my post from being so messy.. sorry if I failed miserably.. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions