Site method issue

I’ve turned my sites menu into a site method to keep the snippet tidy. However, im getting a weird issue where a zero character is being created before the list item. I cant for the life of me figure out where that is coming from.

  'siteMethods' => [
      'sitemenu' => function ($btnclass = 'btn-small', $activeclass = 'active') {
          $sitemenu = $this->menu()->toStructure();
          $menu = '';
          foreach ($sitemenu as $menu) {
            $link = $menu->link()->toLinkObject();
            $active = '';
            if ($link->type() == 'page') {
                $active = ($this->find($menu->link()->toLinkObject()->value())->isOpen()) ? $activeclass : '';
            $link = Html::a($link->url(), $link->title(), ['title' => $link->title(), 'class' => $btnclass]);
            $li = Html::tag('li', [$link], ['class' => $active]);
            $menu .= $li;
          return $menu;

Any idea? The menu links are in a structure field, and it makes use of the link-field plugin.

May be:
you use TWO different “$menu”. Can you change one?

That wouldnt do it. The first one is empty, the second appends to it in the loop. Nothing wrong there.

Yes, you have to change one of your $menu variables. Either the one you add to (starting with the $menu = ''; definition), or the one in your loop

If you don’t you add the $li elements to the structure object, which doesn’t make sense at all.

1 Like

The first should get the html of the menu I think.
But it is hidden in the foreach loop.

Oh i see… becuase i used the same name on the loop. I get it :slight_smile: It’s gone now. Thanks.

Need :eyeglasses: