Mecha CMS

Mecha CMS blog and documentation.

Class Menu

Updated: Sunday, 07 August 2016

Generate menus from an array input.

Convert array into menu list.

Configuration

Menu::$config = array(
    'classes' => array(
        'parent' => 'parent',
        'child' => 'child child-%d',
        'current' => 'current',
        'separator' => 'separator'
    )
);

Usage

Menu::add('my_menu', array(
    'Home' => '/',
    'About' => '/about',
    'Contact' => '/contact'
));

echo Menu::my_menu();

Related: Class Tree

Result:

<ul class="parent">
  <li><a href="http://mecha-cms.com/2016/home">Home</a></li>
  <li><a href="http://mecha-cms.com/2016/about">About</a></li>
  <li><a href="http://mecha-cms.com/2016/contact">Contact</a></li>
</ul>

Nested Menu List

Menu::add('my_nest_menu', array(
    'Menu 1' => '/test-1',
    'Menu 2' => '/test-2',
    'Menu 3' => array(
        'Menu 3.1' => '/test-3-1',
        'Menu 3.2' => '/test-3-2'
    )
));

echo Menu::my_nest_menu();

Result:

<ul class="parent">
  <li><a href="http://mecha-cms.com/2016/test-1">Menu 1</a></li>
  <li><a href="http://mecha-cms.com/2016/test-2">Menu 2</a></li>
  <li class="ul">
    <span class="a">Menu 3</span>
    <ul class="child child-1">
      <li><a href="http://mecha-cms.com/2016/test-3-1">Menu 3.1</a></li>
      <li><a href="http://mecha-cms.com/2016/test-3-2">Menu 3.2</a></li>
    </ul>
  </li>
</ul>

Active Parent Menu

Normally, the generated list items will produce some span elements in the parent menu with a class of a. This way, you can activate the parent menu:

Menu::add('test_parent', array(
    'Parent 1' => array( # ← inactive!
        'Child 1.1' => '/test-1-1',
        'Child 1.2' => '/test-1-2'
    ),
    'Parent 2 (/foo)' => array( # ← active!
        'Child 2.1' => '/foo/test-2-1',
        'Child 2.2' => '/foo/test-2-2'
    )
));

echo Menu::test_parent();

Result:

<ul class="parent">
  <li class="ul">
    <span class="a">Parent 1</span> ← inactive!
    <ul class="child child-1">
      <li><a href="http://mecha-cms.com/2016/test-1-1">Child 1.1</a></li>
      <li><a href="http://mecha-cms.com/2016/test-1-2">Child 1.2</a></li>
    </ul>
  </li>
  <li class="ul">
    <a href="http://mecha-cms.com/2016/foo">Parent 2</a> ← active!
    <ul class="child child-1">
      <li><a href="http://mecha-cms.com/2016/foo/test-2-1">Child 2.1</a></li>
      <li><a href="http://mecha-cms.com/2016/foo/test-2-2">Child 2.2</a></li>
    </ul>
  </li>
</ul>

Be confidence to enter any link!

Menu::add('links', array(
    'External Link 1' => 'http://www.google.com',
    'External Link 2' => 'http://www.yahoo.com'
));

echo Menu::links();

Result:

<ul class="parent">
  <li><a href="http://www.google.com">External Link 1</a></li>
  <li><a href="http://www.yahoo.com">External Link 2</a></li>
</ul>
Donation and Email Subscription