Răsfoiți Sursa

Now, if a submenu is empty, it will not be rendered

Artur 6 ani în urmă
părinte
comite
a8051dda4d
2 a modificat fișierele cu 14 adăugiri și 3 ștergeri
  1. 4 0
      index.php
  2. 10 3
      src/MenuBuilder/MenuItem.php

+ 4 - 0
index.php

@@ -16,4 +16,8 @@ $sidebar->create_submenu('administrativo', 'Administrativo');
 
 $sidebar->add_on_new('basico', '', 'Geografico', '', Array(), -1);
 
+$sidebar->create_submenu('teste', 'Teste');
+//$sidebar->add_on_new('teste', '', 'Elemento');
+
+
 echo $sidebar->render();

+ 10 - 3
src/MenuBuilder/MenuItem.php

@@ -67,6 +67,7 @@ class MenuItem {
         $this->link = $link;
         return $this;
     }
+
     /**
      * Set the menu item name.
      * 
@@ -77,6 +78,7 @@ class MenuItem {
         $this->name = $name;
         return $this;
     }
+
     /**
      * Set the menu item title.
      * 
@@ -119,7 +121,7 @@ class MenuItem {
         $this->nested = $menu;
         return $this;
     }
-    
+
     /**
      * Nest another menu.
      * 
@@ -128,7 +130,7 @@ class MenuItem {
      */
     public function nested() {
         return $this->nested;
-    }    
+    }
 
     /**
      * Render the menu item.
@@ -136,7 +138,12 @@ class MenuItem {
      * @return string
      */
     public function render() {
-        return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . $this->renderNestedMenu() . '</li>';
+        if( empty($this->nested) ){
+            return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . '</li>';
+        }
+        if ( !empty($this->nested->items()) ) {
+            return '<li>' . $this->html->link($this->link, $this->title, $this->icon, $this->attributes) . $this->renderNestedMenu() . '</li>';
+        }
     }
 
     /**