Recognize the first UL

Hi,
i have a little problem. How can I recognize that the UL is the first one to give it another classes as the sublevel UL?

<?php if(!isset($subpages)) $subpages = $site->pages()->children() ?>
<ul <?php if(): ?> class="nav nav-clear navbar-stacked">
	<?php foreach($subpages->visible() AS $p): ?>
	<li role="presentation" class="depth-<?php echo $p->depth(), ($p->isActive()) ? ' active' : '' ?>">
		<a href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
		<?php if($p->hasChildren()): ?>
		<?php snippet('sidebar_menu', array('subpages' => $p->children())) ?>
		<?php endif ?>
	</li>
	<?php endforeach ?>
</ul>

BR

I assume that code is actually the sidebar_menu snippet that you’re recursively calling later? You could pass the current recursion depth as a parameter.

Alternatively, depending on what your styles actually do and whether you need the class names on the elements or not, the :first-of-type CSS selector might be enough. More info on CSS-tricks

1 Like

Hello Fabian,

Yes it is.
I know how I can pass a parameter with the snippet, but how can I count the recursion?

Kind regards,
Jan

You can pass 'count' => 1 on your first call and then 'count' => $count++ in the recursive call. Now you only need to check if $count == 1.

Thanks but it doesn’t work like I do it. Here the snippetcall inside the template

	<?php snippet('sidebar_menu', array('depth' => 1)) ?>

Inside the snippet:

<?php if(!isset($subpages)) $subpages = $site->pages()->children() ?>
<?php $depth = $depth?>	
<ul <?php if( $depth == 1 ): ?> class="nav nav-clear navbar-stacked"<?php endif ?>>
<?php foreach($subpages->visible() AS $p): ?>
<li role="presentation" class="depth-<?php echo $p->depth(), ($p->isActive()) ? ' active' : '' ?>">
	<a href="<?php echo $p->url() ?>"><?php echo $p->title() ?></a>
	<?php if($p->hasChildren()): ?>
	<?php snippet('sidebar_menu', array('subpages' => $p->children(), 'depth' => $depth++)) ?>
	<?php endif ?>
</li>
<?php endforeach ?>

I do something wrong because the child UL’s got also the classes.

Aaah, sorry. I always struggle with pre- and post-increment in those kinds of situations. Check out this instead:

<?php snippet('sidebar_menu', array('subpages' => $p->children(), 'depth' => ++$depth)) ?>
```

This increments `$depth` *before* passing it to the snippet, so that the recursive call works with the new, incremented value.

Alternatively, I just discovered that [pages have a depth][1]. You can probably work with that and get rid of passing around recursion depths altogether. 


  [1]: http://getkirby.com/docs/cheatsheet/page/depth