Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 173033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:16:16+00:00 2026-05-11T13:16:16+00:00

I found a couple of ways to handle recursion in Smarty, mostly based on

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:16:17+00:00Added an answer on May 11, 2026 at 1:16 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 252k
  • Answers 252k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you really have to think about the risk… May 13, 2026 at 9:48 am
  • Editorial Team
    Editorial Team added an answer Check that the user running the PHP script has permission… May 13, 2026 at 9:48 am
  • Editorial Team
    Editorial Team added an answer Enable Tools > Options > Text Editor > C# >… May 13, 2026 at 9:48 am

Related Questions

In the past I've worked with -Wall and other switches for gcc to eliminate
I'm working on an application for a charitable student organization. The application will track
In the past I've never been a fan of using triggers on database tables.
I prefer to use OOP in large scale projects like the one I'm working

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.