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

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes it is possible to create such functionality. You will… May 13, 2026 at 10:23 am
  • Editorial Team
    Editorial Team added an answer If you are willing to use MMX... MMX gives you… May 13, 2026 at 10:23 am
  • Editorial Team
    Editorial Team added an answer It's because you're including dbo. as part of the table… May 13, 2026 at 10:23 am

Related Questions

I have a small problem with Smarty... I have two different template files in
I have a php page that produces an array of elements. For the sake
I'm building a web application that uses LAMP and the Smarty template framework. The
In the past I've written sites in ASP.NET just to get nice reusable templating
In RC1 the behavior of the template for creating a view changed. As explained

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.