Search the Community
Showing results for tags 'recursive'.
-
Aligator (wip) Processwire Module to render a nested tree starting from a single root or an array of pages. Aligator is similar to MarkupSimpleNavigation but has a different approach of how to define the markup for your menu. It doesn't assume any markup or classes. It's up to you to define them where needed. It's less plug and play and requires some more advanced knowledge of ProcessWire, as some additional setup and coding is needed. But allows for powerful and easier customization without using hooks. Aligator uses callback functions to achieve this. Additionally a selector can be used to filter the children for your navigation. Note: This module is a fun project trying to find simple configurable method to render navigations. It's a work in progress and there might be major changes to how the module works. See further infos and examples on the repository: https://github.com/somatonic/Aligator
- 15 replies
-
- 21
-
- nested
- navigation
-
(and 2 more)
Tagged with:
-
Hi there, i just want to share the code of what the subject line says. The pages to appear in the menu have a custom field "in_main_menu" of type checkbox checked. The item <li>s of the actually shown page and in the direttissima upwards - except home - get the class "current". The item <ul>s of submenus get the class "submenu". This is basically Ryans Code from here run through a meatgrinder. I hope useful for any beginners like me looking for a solution to a similar problem. Any improvement welcome! Now the code: /** * A recursive menu containing home. (It is added to the list of menu items while $level =1). * In order to be included, a page must have a custom field "in_main_menu" of type "checkbox", * and the latter has to be checked. * All submenu <ul>s get class "submenu". * All <li>s in the direttissima upwards of the page shown - except home - get the class "current". * Takes $root out of the recursion in order to not have all pages twice. * All menu items, that have children, do not have a link to their content, but act merely as switches. * You can switch this behaviour off by replacing * * $href = ( $item == $root || !$item->numChildren ? "$item->url" : "#" ); * * with * * $href = $item->url; * * Or you can make it conditional by querying a second custom field "has_own_page", e.g. * * $href = ( $item == $root || !$item->numChildren || $item->has_own_page ? "$item->url" : "#" ); * * Intended to be used without arguments, i.e. "selectiveMenu();" , i.e. always starting with site root. */ function selectiveMenu(Page $root = null) { $shownPage = wire('page'); if(is_null($root)) $root = wire('pages')->get('/'); $level = count($root->parents); $ul_class_string = (($level > 0) ? "class='submenu'" : ""); $out = "\n<ul {$ul_class_string}>"; $parents = $shownPage->parents; $items = $root->children; if ($level == 0) $items->prepend($root); foreach( $items as $item) { if ($item->in_main_menu) { $s = ''; $li_class_string = ( ( $parents->has($item) && $item !== $root ) || $item === $shownPage ? "class='current'" : "" ); if($item->numChildren && $item !== $root) { $s = str_replace("\n", "\n\t\t", selectiveMenu($item)); } $href = ( $item == $root || !$item->numChildren ? "$item->url" : "#" ); $out .= "\n\t<li {$li_class_string}>\n\t\t<a href='{$href}'>{$item->title}</a>$s\n\t</li>"; } } $out .= "\n</ul>"; return $out; }
- 1 reply
-
- 1
-
- navigation
- recursive
-
(and 1 more)
Tagged with:
-
I have been banging my head about this for a while so I reckoned let me pick your brains instead I have a recursive function pinched from SO that loops over and outputs some records... <?php function listProducts($productCat,$level=0) { $result = wire('db')->query("SELECT id, product, productcat FROM products WHERE productcat='$productCat' ORDER BY subcat"); if($result->num_rows == 0) return; echo "<ul class='sortable'>\n"; while (list ($id, $product) = $result->fetch_row()) { echo "<li id='item_{$id}'>\n"; echo "<div class='dd-handle'>{$product}</div>\n"; listProducts($id,$level+1); echo "</li>\n"; } echo "</ul>\n"; } ?> <div> <h5>Products</h5> <ul class="sortable"><?php echo listProducts(0);//call the function ?></ul> <!-- problem --> </div> Code works just fine. However, what I need to do is to conditionally output the <ul> in the function depending on whether records are found or not like so: if($result->num_rows == 0) { echo "<ul class='sortable'></ul>\n";// echo empty ul if no products found } else { echo "<ul class='sortable'>\n";// echo opening ul tag if products found } This is because I am adding product items to the <ul> using jQuery. If there are no products, I need an empty <ul></ul> to append my product items to. If there are product items, no need to echo the empty <ul></ul>, just echo the opening <ul> tag and the function will close it properly later on. If I do it like this, the code doesn't work properly. If there are no results, I don't get my empty <ul></ul>. If there are records, I am getting extra/unnecessary empty <ul></ul> after each <li></li>. I have tried different things including $result->fetch_array (); then doing an if against that but without success. If I manually add the empty <ul></ul> when I call the function (like in my example above) I will of course still get empty extra <ul></ul> when I call the function and there are records. It's either this or resorting to querying the db again outside the function to check if there are records and echoing empty <ul></ul> if there aren't...etc. Hope this makes sense and thanks for your help. Sorry for the long-winded <ul>s!