views:

1021

answers:

4

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.

array(
    array(
     'id'=>0,
     'headline'=>"My parent headline",
     'body'    =>"My parent body",
     'children'=>array(
      array(
       'id'=>1,
       'headline'=>"My firstChild headline",
       'body'    =>"My firstChild body",
       'children'=>array()
      ),
      array(
       'id'=>2,
       'headline'=>"My secondChild headline",
       'body'    =>"My secondChild body",
       'children'=>array()
      )
     )
    ),
);

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:

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

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?

+1  A: 

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.

Kent Fredric
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
+1  A: 

The best way is not to do it.

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

Allain Lalonde
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
Fair enough, but compared to doing recursion with a one-off function, a smarty solution will be convoluted.
Allain Lalonde
A: 

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...

michal kralik
+2  A: 

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

Just kidding. This should do what you want:

<?php
/*
* 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!

GloryFish