Conditionally Adding Items

v1.1+

The add method has an equivalent method with an if suffix, which takes a condition as it's first argument. The items will only be added to the menu if the condition resolves to true (non-strict).

Menu::new()
    ->add(Link::to('/', 'Home'))
    ->addIf(!$loggedIn, Link::to('/login', 'Login'))
    ->addIf(!$loggedIn, Link::to('/register', 'Register'))
    ->addIf($loggedIn, Link::to('/logout', 'Logout'))
<!-- $loggedIn = false -->
<ul>
    <li><a href="/">Home</a></li>
    <li><a href="/login">Login</a></li>
    <li><a href="/register">Register</a></li>
</ul>
<!-- $loggedIn = true -->
<ul>
    <li><a href="/">Home</a></li>
    <li><a href="/logout">Logout</a></li>
</ul>

This also applies to the convenience methods:

Menu::new()
    ->link('/', 'Home')
    ->htmlIf(count($notifications), '<i data-notifications data-icon="bell"></i>');
<!-- $notifications = [] -->
<ul>
    <li><a href="/">Home</a></li>
</ul>
<!-- $notifications = ['new-message'] -->
<ul>
    <li><a href="/">Home</a></li>
    <li><i data-notifications data-icon="bell"></i></li>
</ul>