2

Can someone help me to translate this code into smart friendly, taken from Here

function outputCategories($categories, $startingLevel = 0)  
{

    foreach ($categories as $key => $category)
    { 
        if (count($category['children']) > 0)
        {
            echo '<li><a title="'.$category['name'].'" href="'.$category['url'].'">'.$category['name'].'</a><ul>';
            outputCategories($category['children'], $startingLevel+1);
            echo "</li>";
        }
        else
        {
            echo '<li><a title="'.$category['name'].'" href="'.$category['url'].'">'.$category['name'].'</a></li>';
        }
    }
    echo "</ul>";
    return self;
}

I'm able to generate single level categories using this lines:

{foreach from=$hsitemap item=c name=hsitemap}
 {if $c.parent_id ==0 }
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
    {foreach item=d from=$c.children name=sitemap} 
<li><a title="{$d.site_name}" href="{$d.site_url}">{$d.site_name}</a></li>
    {/foreach}
{else}  
<li><h2><a title="{$c.site_name}" href="{$c.site_url}">{$c.site_name}</a></h2><ul>
{/if}
</ul>
</li>
{/foreach}  
</ul>

But now I have a requirement for multilevel, kindly help me with this.

Community
  • 1
  • 1

2 Answers2

0

you should make an include-file which includes itself every new level... nest the loop to a new file or create a smarty-function, then include it or call the smarty-function, and IN IT, call itself (or call the function)...

TheHe
  • 2,933
  • 18
  • 22
  • Hmmm, including smarty function inside smarty..never done that...ll search for some examples.. – lonelycrypto Aug 23 '12 at 06:07
  • It's a silly solution. In smarty you can create functions: http://www.smarty.net/docs/en/language.function.function.tpl – uzsolt Aug 23 '12 at 10:13
  • as i said... recursion with includes OR function if you have php-access to the layer -- – TheHe Aug 23 '12 at 10:29
  • Sorry, I was careless. But the "recursive" including is silly solution. Does it have any advantage? – uzsolt Aug 23 '12 at 19:12
  • 4sure it doesn't... but, as i thought he didn't know, that you can define functions... maybe he could be a designer who doesn't have access to the php-core of the system and cannot define new funtions... smarty-template-recursion is not the best, but is easy understandable and will work. – TheHe Aug 23 '12 at 19:50
0

You can create functions which can call itself, see example 7.43. in documentation.

uzsolt
  • 5,832
  • 2
  • 20
  • 32
  • Thank you, I'm aware of it.. The problem I'm facing now is that I'm unable to call the function inside {block name="content"}, which shows a blank page.. – lonelycrypto Aug 27 '12 at 06:50