Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Function recursive

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
staff76620
Smarty n00b


Joined: 26 May 2016
Posts: 1

PostPosted: Thu May 26, 2016 12:32 pm    Post subject: Function recursive Reply with quote

Hello I have a recursive function in php and I integrate into smarty but I have a problem.

Here's my php

Code:

function generate_menu($parent = 0)
    {
        $has_childs = false;
      $html = "";

        //this prevents printing 'ul' if we don't have subcategories for this category
      global $menuItems;
        global $parentMenuIds;

      //use global array variable instead of a local variable to lower stack memory requierment
        foreach($menuItems as $key => $menuItem)
        {
         if ($menuItem['id_parent'] == $parent)
            {   
            //if this is the first child print '<ul>'
               if ($has_childs === false)
               {
               //don't print '<ul>' multiple times 
               $has_childs = true;
                    if($parent != 0)
                    {
                  $html .= '<ul class="dropdown-menu">';
                    }
                }
            
            if($menuItem['id_parent'] == 0 && in_array($menuItem['id'], $parentMenuIds))
            {
               $html .= '<li class="dropdown"><a class="dropdown-toggle" href="#">' . $menuItem['name'] . '</a>';
            }
            else if($menuItem['id_parent'] != 0 && in_array($menuItem['id'], $parentMenuIds))
            {
               $html .= '<li class="dropdown-submenu"><a href="#">' . $menuItem['name'] . '</a>';
            }
            else
            {
               $html .= '<li><a href="#">' . $menuItem['name'] . '</a>';
            }
            
            $html.= $this->generate_menu($menuItem['id']);
                               
            //call function again to generate nested list for subcategories belonging to this category
            $html .= '</li>';
         }
      }
      
      if ($has_childs === true) $html .= '</ul>';
                  
      return $html;
   }


Here in smarty

Code:

{function menu parent=0}
                                          {foreach $menuItems as $menuItem}
                                             {if is_array($menuItem)}
                                                {if $menuItem['id_parent'] eq $parent}
                                                   {if $has_childs === false}
                                                      {$has_childs=true}
                                                      {if {$parent} neq 0}
                                                      <ul class="dropdown-menu">
                                                      {/if}
                                                   {/if}
                                                   
                                                   {if $menuItem['id_parent'] eq 0 && in_array($menuItem['id'], $parentMenuIds)}
                                                      <li class="dropdown"><a class="dropdown-toggle" href="#">{$menuItem['name']}</a>
                                                   {elseif $menuItem['id_parent'] neq 0 && in_array($menuItem['id'], $parentMenuIds)}
                                                      <li class="dropdown-submenu"><a href="#">{$menuItem['name']}</a>
                                                   {else}
                                                      <li><a href="#">{$menuItem['name']}</a>
                                                   {/if}
                                                   

                                                   </li>
                                                {/if}
                                             {/if}
                                          {/foreach}
                                          
                                          {if $has_childs === true}</ul>{/if}
                                          
                                       {/function}
                                       
                                       {menu data=$menu}


How to call a $this->generate_menu($menuItem['id']) in Smarty ?

Cordially
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP