Home Β» Php Β» php – What is the best way to handle recursion in smarty?

php – What is the best way to handle recursion in smarty?

Posted by: admin July 12, 2020 Leave a comment


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?

How to&Answers:

“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!


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.


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…


The best way is not to do it.

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