DennisA
December 12, 2017, 3:21pm
1
Hi, I want to create a simple menu with 3 links. One of them should be a drop-down with links to each subpage.
The structure should be like this:
I tried many different options but couldnt find a solution which only shows the pagename for the dropdown link.
<header>
<?php $items = $pages->visible(); if($items->count()): ?>
<nav>
<?php foreach($pages->visible() as $item): ?>
<li <?php e($item->isOpen(), ' class="active" ') ?> >
<a href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
<?php if($item->hasChildren()): ?>
<ul class="sublink">
<?php foreach($item->children() as $child): ?>
<li><a href="<?= $child->url() ?>"><?= $child->title()->html() ?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
</li>
<?php endforeach ?>
</nav>
<?php endif ?>
</header>
texnixe
December 12, 2017, 3:41pm
2
This should output what you want:
<header>
<?php $items = $pages->visible(); if($items->count()): ?>
<nav>
<?php foreach($items as $item): ?>
<li <?php e($item->isOpen(), ' class="active" ') ?> >
<?php if($item->hasChildren()): ?>
<?= $item->title()->html() ?>
<ul class="sublink">
<?php foreach($item->children() as $child): ?>
<li><a href="<?= $child->url() ?>"><?= $child->title()->html() ?></a></li>
<?php endforeach ?>
</ul>
<?php else: ?>
<a href="<?= $item->url() ?>"><?= $item->title()->html() ?></a>
<?php endif ?>
</li>
<?php endforeach ?>
</nav>
<?php endif ?>
</header>
1 Like
DennisA
December 12, 2017, 3:42pm
3
is working perfectly… .thank you
texnixe
December 12, 2017, 3:43pm
4
<?php $items = $pages->visible(); if($items->count()): ?>
<nav>
<?php foreach($pages->visible() as $item): ?>
Note that you defined $items
but then didn’t use that variable at all…
1 Like
DennisA
December 12, 2017, 3:46pm
5
Uh I see … Thanks again!!!
Is there an example on how to append the “active” class without writing the whole class="active"
…
also is the e() a CakePHP function? is this still best practice?
e()
is a Kirby helper that echos based on the result of an if set as the first parameter. See here for the Kirby 2 version, but it is also possible with Kirby 3 .
Thanks for the feed back…
I should be able to do something like this
$classList = ['myClass', 'secondaryClass'];
if ($item->isOpen()){
$classList[] = 'active';
}
<li class="<?= implode(' ', $classList) ?>">
I’ll give it a try!
Should be
<li class="<?= implode(' ', $classList) ?>">