Snippet: Template logic to display dynamic left menu

I found that another user used this source code with success in the forums, so I thought we all might benefit from the documentation.

Question

I made up a site using a top and a left menu. In the left one there is only one menu level shown. Now I want the next level been shown, too.

Answer

You could try something like this

{def $second_level_children=array()}
{def $root_node_children=fetch('content', 'list', hash('parent_node_id', 2))}

{if gt($root_node_children|count,0)}
<ul>
{foreach $root_node_children as $child}
<li>
<a href={$child.url_alias|ezurl()}>{$child.name|wash()}</a>
{if or(eq($child.node_id,$module_result.node_id),eq($module_result.path.1,$child.node_id))}
{set $second_level_children=fetch('content','list',hash('parent_node_id', $child.node_id))}
{if gt($second_level_children|count,0)}
<ul>
{foreach $second_level_children as $child2}
<li><a href={$child2.url_alias|ezurl()}>{$child2.name|wash()}</a></li>
{/foreach}
</ul>
{/if}
{/if}
<li>
{/foreach}
</ul>
{/if}

First fetch the elements of the top level, foreach element check if it's the node that is being viewed or if it's one of its children's node. If so, show its children.
The code above has not been tested, but maybe would give you an idea on how to do it.

References