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

  • Home
  • SEARCH
  • 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 540651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:15:44+00:00 2026-05-13T10:15:44+00:00

I have a Smarty template file in which I’m looping over hierarchical data represented

  • 0

I have a Smarty template file in which I’m looping over hierarchical data represented by nested arrays. The child data is represented exactly the same way as the parent data, so I wanted to recursively {include} the Smarty template to render this:

Source of “my.tpl”:

{foreach from=$children item="child" name="childrenLoop"}
    {* display stuff here *}
    {if $smarty.foreach.childrenLoop.last}
        {* do something special here when it is the last item *}
    {/if}

    {assign var="grandChildren" value=$child->getChildren()}
    {if $grandChildren|@count > 0}
        {include file="my.tpl" children=$grandChildren}
    {/if}
 {/foreach}

My problem is that when the {include} finishes executing, $smarty.foreach.childrenLoop.* tells me about the {include}'s loop, not the current loop. So if the inner loop had 10 items and the outer loop has 5, {$smarty.foreach.childrenLoop.total} will be 10 when I exit the inner loop, rather than 5. Needless to say, this is messing up my output.

I thought I could get around this problem by suffixing the loop with a value passed along by the include. I seem to be able to name the loop correctly:

{foreach from=$children item="child" name="childrenLoop_`$suffix`"}

… but I can’t figure out how to access its properties, and the Smarty manual is not helpful:

{$smarty.foreach.childrenLoop_$suffix.total} {* NOPE! *}
{$smarty.foreach[childrenLoop_$suffix].total} {* NOPE! *}

What can I do here?

  • 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. Editorial Team
    Editorial Team
    2026-05-13T10:15:44+00:00Added an answer on May 13, 2026 at 10:15 am

    Maybe try

    {assign var="chSuffix" value="childrenLoop_$suffix"}
    {foreach from=$children item="child" name="$chSuffix"}
        {$smarty.foreach.$chSuffix.total}
    {/foreach}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SOLVED: Error in template file I have Smarty setup like this: require_once 'smarty/Smarty.class.php'; $smarty
I have page for which I want to load different template file for different
I have following codes using smarty template engine In php file: $smarty->assign('SITE_URL', 'http://localhost/mis/'); In
I have JavaScript in my template file, because of the smarty can't include the
I have a small problem with Smarty... I have two different template files in
I have the following string in the smarty (php templating system) variable $test: <img
Have just started using Visual Studio Professional's built-in unit testing features, which as I
On a PHP site using the smarty template engine (or at least a close
I'm building a web application that uses LAMP and the Smarty template framework. The
What is the strategy in smarty for using different variables each time a template

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.