Take the 2-minute tour ×

I found a couple of ways to handle recursion in Smarty, mostly based on including templates into themselves, which seems like ridiculous waste of resources. I found one solution, by Messju over at Smarty that seemed to be just right - but it is not supported and fails in the latest version of smarty :(

For people asking: What I want smarty to print out is a discussion thread that is defined by an array of entries. If an entry has one or more answers, those are listed as children to said entry in an array, and so on.

    	'headline'=>"My parent headline",
    	'body'    =>"My parent body",
    			'headline'=>"My firstChild headline",
    			'body'    =>"My firstChild body",
    			'headline'=>"My secondChild headline",
    			'body'    =>"My secondChild body",

The nested array has an arbitrary depth, and each entry will have an arbitrary number of children. To me this is something I want to do with within the scope of the template, as I consider it pure display logic. I do not want to have to handle HTML or some type of HTML placeholders outside of the template.

I want smarty to print this as nested lists:

    	<h1>My parent headline</h1>
    	<p>My parent body</p>
    			<h1>My firstChild headline</h1>
    			<p>My firstChild body</p>
    			<h1>My secondChild headline</h1>
    			<p>My secondChild body</p>

I'm starting to realize this might be a very case-by-case problem, so I figure I'll just write a smarty plugin to handle this specifically, although I'd rather have an all-around solution.

Is there a way?

share|improve this question

5 Answers 5

up vote 6 down vote accepted

"In order to understand recursion, you must first understand recursion..."

Just kidding. This should do what you want:

* Smarty plugin
* File:     function.recurse_array.php
* Type:     function
* Name:     recurse_array
* Purpose:  prints out elements of an array recursively

function smarty_function_recurse_array($params, &$smarty)

if (is_array($params['array']) && count($params['array']) > 0) {
   $markup = '';

   $markup .= '<ul>';

   foreach ($params['array'] as $element) {
      $markup .= '<li>';

      $markup .= '<h1>' . $element['headline'] . '</h1>';
      $markup .= '<p>' . $element['body'] . '</p>';

      if (isset($element['children'])) {
         $markup .= smarty_function_recurse_array(array('array' => $element['children']), $smarty);

       $markup .= '</li>';

   $markup.= '</ul>';

   return $markup;

} else {
   return 'not array';

Place the file into your smarty/plugins folder. Assign your array to Smarty then call it in your template like so:

{recurse_array array=$data}

Here's nice tutorial for making custom Smarty functions:

Creating Custom Smarty Functions

Be aware of the dependency that this example has on your underlying data structure. Also, keep in mind that an unusually long or deeply nested set of data could be really slow. Manage your complexity, keep things well documented, and you should be fine. Good luck!

share|improve this answer

With Smarty 3, this can be done using {function}. The following code will produce the required ouput.

{function name=printList}
    {foreach $items as $item}
        {if $item['children']}
            {call name=printList items=$item['children']}

{call name=printList items=$comments}

More information can be found at the docs.

Side note: Just because something is complex or recursive it doesn't mean that it can't be inside a template. For God's sake the HTML ul-li structure is naturally recursive and by hiding it away or moving it somewhere else (just because it is too complex for a template) you are introducing an extra complexity into the application.

share|improve this answer
Fantastic! I know it is an old question but this approach is brilliant. +1 –  Vladimir Hraban Jul 30 '13 at 15:18

You might want to consider creating custom function/modifier/plugin for smarty. Pass the array to the custom function along with defining what is the template the function should use. If it is that simple, only to insert a text to certain place, load the template within function and in PHP work with the template using regexes/str_replace/...

Or do it directly in PHP without using smarty templates, because all you need is h1, ul, li and p tags and to change the layout use CSS.

Or if your concern is the overhead with opening and closing files in Smarty, estimate what is the amount of levels in 90% of cases and create template which will cover those 90%. For the rest use recursion by including the template itself...

share|improve this answer

The best way is not to do it.

Smarty is supposed to be simple. This deesn't sound it.

share|improve this answer
Think of it this way, the app needs to display an arbitrarily nested list of stuff. There's complexity there. You need to manage complexity. It makes sense to put what is essentially display logic (loop through an array and spit out markup) in the template. Just being recursion doesn't make it evil. –  GloryFish Mar 16 '09 at 20:58
Fair enough, but compared to doing recursion with a one-off function, a smarty solution will be convoluted. –  Allain Lalonde Mar 17 '09 at 4:17

You might need to elaborate on what you are doing exactly, code samples, requirements, because as it stands, the very IDEA that you have complicated code in smarty enough to enter recursion frankly scares me.

AKA: I vote NOT to be working on your project based on information provided.

share|improve this answer
I figured my specific case would be of little interest to other people trying to find a solution, and thus posted a more general question in hopes of a general answer. Given that the general answer seems to be not to do it, I posted more details :) –  Jacob Hansson Jan 13 '09 at 15:23
I don´t think printing a TREE in Smarty should be such a scary idea. –  Danita Mar 14 '12 at 12:43
Printing a tree in smarty is a dangerous fusion of presentation and logic. Its much better to weed the tree logic out externally somehow and consume it in the template ( such as a custom function ). Doing it in the template itself is just insane. –  Kent Fredric Mar 27 '12 at 21:56

Your Answer


