children of current page

4 views
Skip to first unread message

93-interactive

unread,
Dec 5, 2011, 10:23:42 AM12/5/11
to Zotonic developers
Hi,

zotonic 0.7.1:

from within a template, how can i get the children of the current
page, as defined in the main_menu?

Andreas Stenius

unread,
Dec 6, 2011, 5:38:40 AM12/6/11
to zotonic-d...@googlegroups.com
You can get the complete menu structure from the menu property of a
menu resource.

Example for the main_menu:

1: {% print m.rsc.main_menu.menu %}
2: {% print m.rsc.main_menu.menu|menu_flat %}

Sample output:

1: [{360,[]},{316,[{314,[]},{315,[]}]}]

2: [{360,[1],undefined},
{316,[2],down},
{314,[1,2],undefined},
{315,[2,2],undefined},
{undefined,undefined,up}]

Which you the may traverse with a for loop, looking for you current
id, and the from there you have the children...
I leave it as an exercise... ;c)

//Andreas

2011/12/5 93-interactive <93inte...@googlemail.com>:

93-interactive

unread,
Dec 6, 2011, 8:20:27 AM12/6/11
to Zotonic developers
Hi,

On Dec 6, 11:38 am, Andreas Stenius <andreas.sten...@astekk.se> wrote:
> You can get the complete menu structure from the menu property of a
> menu resource.
>
> Example for the main_menu:
>
> 1: {% print m.rsc.main_menu.menu %}
> 2: {% print m.rsc.main_menu.menu|menu_flat %}
>
> Sample output:
>
> 1: [{360,[]},{316,[{314,[]},{315,[]}]}]
>
> 2: [{360,[1],undefined},
>  {316,[2],down},
>  {314,[1,2],undefined},
>  {315,[2,2],undefined},
>  {undefined,undefined,up}]
>
> Which you the may traverse with a for loop, looking for you current
> id, and the from there you have the children...
> I leave it as an exercise... ;c)

thank you, unfortunately i still have trouble understanding it, i
should mention, that i am new, both to erlang and zotonic.

As i understand it, result 2 gives me all available id's in list
position [1] and the position within result 1 in list position [2]
(reverse).

So i have two problems here. Result 2 gives me numbers, whereas result
1 gives me names where available and numbers where not, so i can not
match all entries.

Example from my site:

1)

[{page_home,[]},{page_about,[]},{page_contact,[]},{331,[{336,[]},{332,
[]}]}]

2)

[{312,[1],undefined},
{313,[2],undefined},
{314,[3],undefined},
{331,[4],down},
{336,[1,4],undefined},
{332,[2,4],undefined},
{undefined,undefined,up}]

Even if i could, i would still have the problem that i have a reverse
position list, e.g. [1,2] for 314, which within the template, i do not
know how to apply to get the children from result 1.

93-interactive

unread,
Dec 7, 2011, 3:44:13 AM12/7/11
to Zotonic developers
Ok, so after reinstalling everything on the production server and
starting from scratch
for some reason it gets me all numeric indices:

1)

[{316,[{318,[]},{319,[]}]},{317,[]}]

2)

[{316,[1],down},
{318,[1,1],undefined},
{319,[2,1],undefined},
{undefined,undefined,up},
{317,[2],undefined}]

So what i have so far:

{% for page,pos,rest in m.rsc.main_menu.menu|menu_flat %}
{% if page /= undefined %}
{{ page }} on {{ pos }} <br />
{% endif %}
{% endfor %}

what i would need now in instead the {{ page }} on {{ pos }} line
something like

m.rsc.main_menu.menu[pos]

That gives me the children, but i did not find a way to achieve this,
any ideas?

Andreas Stenius

unread,
Dec 7, 2011, 7:43:02 AM12/7/11
to zotonic-d...@googlegroups.com
The flat structure tells you when to go down, and when to come back up.
See the _menu.tpl file for how the menu is rendered in zotonic.
https://github.com/zotonic/zotonic/blob/master/modules/mod_menu/templates/_menu.tpl

So, to only include the children of the current page (if any), you
could do something along the lines of (assuming the current page id is
in variable "id"):

{% for page_id, path, action in m.rsc.main_menu.menu|menu_flat %}

{% if id = page_id and action = "down" %} {# no children unless
action is down #}

{% endif %}

{% endfor %}


Hmm... this becomes quite hard to do with only templating... why not
write a simple filter to return just the children of a given id?

What end result are you looking for?

I didn't quite understand your intention with "m.rsc.main_menu.menu[pos]".

//Andreas

2011/12/7 93-interactive <93inte...@googlemail.com>:

93-interactive

unread,
Dec 8, 2011, 6:00:14 AM12/8/11
to Zotonic developers
Hi,

On Dec 7, 1:43 pm, Andreas Stenius <andreas.sten...@astekk.se> wrote:
> Hmm... this becomes quite hard to do with only templating... why not
> write a simple filter to return just the children of a given id?

because i am new and don't know how to do things on zotonic ;-)

Changed it to filter, got it work, thank you.

Andreas Stenius

unread,
Dec 8, 2011, 7:15:56 AM12/8/11
to zotonic-d...@googlegroups.com
2011/12/8 93-interactive <93inte...@googlemail.com>:
> [...] Changed it to filter, got it work, thank you.

Cool :-)

Reply all
Reply to author
Forward
0 new messages