Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

While-Construct

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Sun May 30, 2004 7:41 pm    Post subject: While-Construct Reply with quote

Hi all,

it would be nice, if smarty includes a while-construct.

I work in a project with Elements with had indefinite Child-Elements and so i have a big problem to loop through all the elements.

I dont know what to do!

Does a workaround exists for those cases?

my arrays look like this:

Code:

array(4) {
  [0]=>
  object(menuheaditem)(7) {
    ["iId"]=>
    string(1) "1"
    ["iLevel"]=>
    int(0)
    ["iStatus"]=>
    string(1) "1"
    ["sLabel"]=>
    string(7) "Artikel"
    ["sLink"]=>
    string(16) "?section=article"
    ["aoChildItems"]=>
    array(2) {
      [0]=>
      object(menusubitem)(7) {
        ["iId"]=>
        string(1) "4"
        ["iLevel"]=>
        int(1)
        ["iStatus"]=>
        string(1) "1"
        ["sLabel"]=>
        string(18) "Artikel hinzufügen"
        ["sLink"]=>
        string(20) "?section=article/add"
        ["aoChildItems"]=>
        array(0) {
        }
        ["sStyle"]=>
        string(9) "STYLE_TOP"
      }
      [1]=>
      object(menusubitem)(7) {
        ["iId"]=>
        string(1) "5"
        ["iLevel"]=>
        int(1)
        ["iStatus"]=>
        string(1) "1"
        ["sLabel"]=>
        string(16) "Artikel anzeigen"
        ["sLink"]=>
        string(21) "?section=article/list"
        ["aoChildItems"]=>
        array(0) {
        }
        ["sStyle"]=>
        string(10) "STYLE_BOTT"
      }
    }
    ["sTableName"]=>
    string(14) "siteadmin_menu"
  }
  [1]=>
  object(menuheaditem)(7) {
    ["iId"]=>
    string(1) "2"
    ["iLevel"]=>
    int(0)
    ["iStatus"]=>
    string(1) "1"
    ["sLabel"]=>
    string(8) "Produkte"
    ["sLink"]=>
    string(17) "?section=products"
    ["aoChildItems"]=>
    array(2) {
      [0]=>
      object(menusubitem)(7) {
        ["iId"]=>
        string(1) "6"
        ["iLevel"]=>
        int(1)
        ["iStatus"]=>
        string(1) "1"
        ["sLabel"]=>
        string(18) "Produkt hinzufügen"
        ["sLink"]=>
        string(21) "?section=products/add"
        ["aoChildItems"]=>
        array(0) {
        }
        ["sStyle"]=>
        string(9) "STYLE_TOP"
      }
      [1]=>
      object(menusubitem)(7) {
        ["iId"]=>
        string(1) "7"
        ["iLevel"]=>
        int(1)
        ["iStatus"]=>
        string(1) "1"
        ["sLabel"]=>
        string(17) "Produkte anzeigen"
        ["sLink"]=>
        string(22) "?section=products/list"
        ["aoChildItems"]=>
        array(0) {
        }
        ["sStyle"]=>
        string(10) "STYLE_BOTT"
      }
    }
    ["sTableName"]=>
    string(14) "siteadmin_menu"
  }
  [2]=>
  object(menuheaditem)(7) {
    ["iId"]=>
    string(1) "3"
    ["iLevel"]=>
    int(0)
    ["iStatus"]=>
    string(1) "1"
    ["sLabel"]=>
    string(6) "Module"
    ["sLink"]=>
    string(16) "?section=modules"
    ["aoChildItems"]=>
    array(1) {
      [0]=>
      object(menusubitem)(7) {
        ["iId"]=>
        string(1) "8"
        ["iLevel"]=>
        int(1)
        ["iStatus"]=>
        string(1) "1"
        ["sLabel"]=>
        string(15) "Module anzeigen"
        ["sLink"]=>
        string(21) "?section=modules/list"
        ["aoChildItems"]=>
        array(0) {
        }
        ["sStyle"]=>
        string(11) "STYLE_ALONE"
      }
    }
    ["sTableName"]=>
    string(14) "siteadmin_menu"
  }
  [3]=>
  object(menuheaditem)(7) {
    ["iId"]=>
    string(1) "9"
    ["iLevel"]=>
    int(0)
    ["iStatus"]=>
    string(1) "1"
    ["sLabel"]=>
    string(4) "Menu"
    ["sLink"]=>
    string(13) "?section=menu"
    ["aoChildItems"]=>
    array(2) {
      [0]=>
      object(menusubitem)(7) {
        ["iId"]=>
        string(2) "14"
        ["iLevel"]=>
        int(1)
        ["iStatus"]=>
        string(1) "1"
        ["sLabel"]=>
        string(20) "Menupunkt hinzufügen"
        ["sLink"]=>
        string(17) "?section=menu/add"
        ["aoChildItems"]=>
        array(0) {
        }
        ["sStyle"]=>
        string(9) "STYLE_TOP"
      }
      [1]=>
      object(menusubitem)(7) {
        ["iId"]=>
        string(2) "10"
        ["iLevel"]=>
        int(1)
        ["iStatus"]=>
        string(1) "1"
        ["sLabel"]=>
        string(21) "Menustruktur anzeigen"
        ["sLink"]=>
        string(18) "?section=menu/list"
        ["aoChildItems"]=>
        array(0) {
        }
        ["sStyle"]=>
        string(10) "STYLE_BOTT"
      }
    }
    ["sTableName"]=>
    string(14) "siteadmin_menu"
  }
}


all elements could have N-Child Elements.

Thanks a lot.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 31, 2004 1:06 am    Post subject: Reply with quote

Hi Kills,

Perhaps this thread on recursion will be useful to you.

Cheers!
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Tue Jun 01, 2004 6:32 am    Post subject: Reply with quote

Hi boots,

thanks a lot.
Its exactly what i'm searching.
kann you help me, with the template i had to build.
how does it look like?

Greets kills
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Tue Jun 01, 2004 7:06 am    Post subject: Reply with quote

Does this looks good?

Code:

{defun name="myMenu" list=$aoMenuItems}
{foreach from=$aoMenuItems item=oMenuItem}
<li>{$oMenuItem->name}</li>
   {if $oMenuItem->hasChilds() }
   <ul>{fun name="MyMenu" list=$oMenuItem->getChilds() }</ul>
   {/if}
{/foreach}
{/defun}



Notice:

Method "hasChilds" checks if the element has Child-Elements
Method "getChilds" returns an array of Elements within all Child-Elements

I can`t test this template because of i have the source only at home.



--------------------





Can you answer me the following questions?

{defun name="myMenu" list=$aoMenuItems} // What does this func do?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jun 01, 2004 7:44 am    Post subject: Reply with quote

That looks about right. Based on your original array structure, you can likely do this slightly more directly (and save you a couple of method calls per iteration):

Code:
{defun name="myMenu" list=$aoMenuItems}
{foreach from=$aoMenuItems item=oMenuItem}
<li>{$oMenuItem->sLabel}</li>
   {fun name="MyMenu" list=$oMenuItem.aoChildItems}</ul>
{/foreach}
{/defun}


This should work since the foreach loop will be a null loop when it gets an empty array.

Quote:
{defun name="myMenu" list=$aoMenuItems} // What does this func do?


This defines the macro function and specifies its calling parameter with its initial value which is used when the macro is first encountered (defun loosely is equivalent to "define function"). In the code above, if $aoMenuItems is initially empty, the macro function should produce no results. This allows you to delay calling the macro if you want to process your array in template (which you probably don't) or if you need to organize the code of a complicated template. ie: define your macro at the top so it doesn't initially execute and then later call it:
Code:
<OL>
{fun name="myMenu" list=$the_real_menu_array}
</OL>


Note that if you do something like that, then you should at least define $aoMenuItems so you don't trigger a notice when the macro function is first encountered.

FWIW, I renamed the function names in my version of the plugin to "macro .. /macro" and "macro_call" since those seem a bit friendlier to me. Another choice that comes to mind is "template_function .. /template_function" and "call". Its not too hard to change messju's plugin to accommodate any names you find agreeable.

The nice thing about these macro functions is that they render tree structures fairly quickly. I find that for best performance, it is best if the macro is defined in the template that defines it. I often track depth on my recursive functions, which is handy, for one, if you want to traverse up to a maximum depth or to handle things like automatically inserting the <OL> .. </OL> only on depth=1, etc.
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Tue Jun 01, 2004 11:41 am    Post subject: Reply with quote

Thanks a lot!!!! Smile
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Fri Jun 04, 2004 2:19 pm    Post subject: Reply with quote

thaths my template:

It wont work Sad

the array $aoMenuStructure is the same as postet in my first reply

[php:1:16d7186312]
{defun name="MenuStructure" list=$aoMenuStructure}
{foreach from=$aoMenuStructure item=oMenuItem}
<li>{$oMenuItem->sLabel}</li>
{fun name="MenuStructure" list=$oMenuItem.aoChildItems}
{/foreach}
{/defun}
[/php:1:16d7186312]

cutout of The Output:

Code:

 <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>

                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
                        <li>Artikel</li>
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Jun 04, 2004 2:31 pm    Post subject: Reply with quote

if $oMenuItem is an object the children are in $oMenuItem->aoChildItems not in $oMenuItem.aoChildItems;
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Fri Jun 04, 2004 2:34 pm    Post subject: Reply with quote

messju wrote:
if $oMenuItem is an object the children are in $oMenuItem->aoChildItems not in $oMenuItem.aoChildItems;


i take the change but it wont help:

Code:

{defun name="MenuStructure" list=$aoMenuStructure}
      {foreach from=$aoMenuStructure item=oMenuItem}
         <li>{$oMenuItem->sLabel}</li>
         {fun name="MenuStructure" list=$oMenuItem->aoChildItems}
      {/foreach}
   {/defun}
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Jun 04, 2004 2:43 pm    Post subject: Reply with quote

{foreach from=$aoMenuStructure item=oMenuItem} should be
{foreach from=$list item=oMenuItem}
otherwise you have an infinite recursion.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Fri Jun 04, 2004 2:47 pm    Post subject: Reply with quote

thats it!!

thank you very mutch!!

Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Jun 04, 2004 3:11 pm    Post subject: Reply with quote

oops, that was my fault Smile I didn't test that code I posted earlier--sorry!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP