View previous topic :: View next topic |
Author |
Message |
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Sun May 30, 2004 7:41 pm Post subject: While-Construct |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon May 31, 2004 1:06 am Post subject: |
|
|
Hi Kills,
Perhaps this thread on recursion will be useful to you.
Cheers! |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Tue Jun 01, 2004 6:32 am Post subject: |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Tue Jun 01, 2004 7:06 am Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jun 01, 2004 7:44 am Post subject: |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Tue Jun 01, 2004 11:41 am Post subject: |
|
|
Thanks a lot!!!! |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Fri Jun 04, 2004 2:19 pm Post subject: |
|
|
thaths my template:
It wont work
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Jun 04, 2004 2:31 pm Post subject: |
|
|
if $oMenuItem is an object the children are in $oMenuItem->aoChildItems not in $oMenuItem.aoChildItems; |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Fri Jun 04, 2004 2:34 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Jun 04, 2004 2:43 pm Post subject: |
|
|
{foreach from=$aoMenuStructure item=oMenuItem} should be
{foreach from=$list item=oMenuItem}
otherwise you have an infinite recursion. |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Fri Jun 04, 2004 2:47 pm Post subject: |
|
|
thats it!!
thank you very mutch!!
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Jun 04, 2004 3:11 pm Post subject: |
|
|
oops, that was my fault I didn't test that code I posted earlier--sorry! |
|
Back to top |
|
|