View previous topic :: View next topic |
Author |
Message |
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Sep 25, 2006 8:02 am Post subject: |
|
|
Adar wrote: | I dont know exactly how it works...but it does |
Beware using things you don't fully understand...it does work, certainly, but you will be greatly served to try to learn why it works. In the meantime, I have to agree: it is very slick work on messju's part |
|
Back to top |
|
Adar Smarty Regular
Joined: 27 May 2004 Posts: 58
|
Posted: Mon Sep 25, 2006 9:49 am Post subject: |
|
|
I dont know exactly how the php-parts do their work (but i can imagine).
But i know exactly how to use it and what recursive-looping is about |
|
Back to top |
|
muhv Smarty n00b
Joined: 07 May 2003 Posts: 3
|
Posted: Tue Nov 28, 2006 5:55 am Post subject: |
|
|
Hi,
One question about {foreach} loop reqursion. How can I do this kind tree-menu:
Code: |
Menu A
Menu B
+ Menu C
+ Menu D
| + Menu F
| L Menu G
+ Menu H
L Menu K
|
I cant determine last item in submenu. Looks like smarty doesnt remember loop name variable after reqursion Is there some other solution to make thos last ending tree elements???? (like Menu G & Menu K )
Thanx! |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Nov 28, 2006 4:07 pm Post subject: |
|
|
muhv wrote: | I cant determine last item in submenu. Looks like smarty doesnt remember loop name variable after reqursion Is there some other solution to make thos last ending tree elements???? (like Menu G & Menu K )
|
I think your question isn't clear. What did you try? I suppose your data is structured to differentiate leafs from non leaf nodes? |
|
Back to top |
|
stot Smarty Rookie
Joined: 13 Nov 2006 Posts: 17
|
Posted: Tue Dec 05, 2006 9:30 am Post subject: Re: Recursion for HTML List |
|
|
Quote: |
This code works but is not very fast. I use it for a bulletin board (http://www.torsten-rentsch.de/pxm.html) where 1000 messages/ thread are not rare. What can i do to improve the recursion speed?
|
I think what you are searching for is something like the nested sets concept to display your message structure (PEAR:DB_nestedSet).
Using this concept, it is possible to retrieve and display a complex and hierarchic structure with exactly one db call.
- scalable
- fast
stot |
|
Back to top |
|
A320 Smarty n00b
Joined: 04 Mar 2007 Posts: 2
|
Posted: Sun Mar 04, 2007 6:40 am Post subject: |
|
|
What is the function to make array like that??
Code: | $Tpl->assign("tree",array("element"=>
array(
array("name"=> "id1","element" =>
array(
array("name" => "id1.1"),
array("name" => "id1.2","element" => array(
array("name" => "id1.2.1"),
array("name" => "id1.2.2")
)
)
)
)
)
)); |
|
|
Back to top |
|
Vicky`s Webgeek Smarty Rookie
Joined: 07 Mar 2007 Posts: 9
|
Posted: Wed Mar 07, 2007 3:42 pm Post subject: |
|
|
Nested while loops make the most sense for this kind of thing
I'd post my code, but I keep getting "You must have 1 posts before you can post URL's/Links." as an error. so hopefully this will solve it. |
|
Back to top |
|
pvginkel Smarty n00b
Joined: 04 Aug 2006 Posts: 3
|
Posted: Wed May 23, 2007 4:00 pm Post subject: |
|
|
muhv wrote: | Hi,
One question about {foreach} loop reqursion. How can I do this kind tree-menu:
Code: |
Menu A
Menu B
+ Menu C
+ Menu D
| + Menu F
| L Menu G
+ Menu H
L Menu K
|
I cant determine last item in submenu. Looks like smarty doesnt remember loop name variable after reqursion Is there some other solution to make thos last ending tree elements???? (like Menu G & Menu K )
Thanx! |
An alternative solution is converting it to a flat array before passing it onto smarty (this is how I do it):
Code: | # Starting out with the original array
$menu = array(
'Menu A' => 'link',
'Menu B' => array(
'Menu C' => 'link',
'Menu D' => array(
'Menu F' => 'link',
'Menu G' => 'link'
),
'Menu H' => 'link',
'Menu K' => 'link'
)
);
# Convert to a flat list
function flatten($menu, $indent = 0) {
$result = array();
foreach ($menu as $key => $value) {
if (is_array($value)) {
# Omission of 'link' means this is a group
$result[] = array(
'indent' => $indent,
'name' => $key
);
$result = array_merge($result, flatten($value, $indent + 1));
} else {
$result[] = array(
'indent' => $indent,
'name' => $key,
'link' => $value
);
}
}
return $result;
}
$smarty->assign('menu', flatten($menu)); |
The template will look something like this:
Code: | {foreach from=$menu item=item}
<div style="margin-left: {$item.indent*10}px;">
{if isset($item.link)}
<a href="{$item.link|escape:url}">{$item.name|escape}</a>
{else}
<b>{$item.name|escape}</b>
{/if}
</div>
{/foreach} |
This (possibly) creates the expected result. In my experience recursion is not necessary in templates. Built in functions would be cool though. |
|
Back to top |
|
HZiegler Smarty Rookie
Joined: 04 Aug 2007 Posts: 5 Location: Germany
|
Posted: Wed Aug 29, 2007 4:31 pm Post subject: |
|
|
You can use this plugin if you want to make a simple or complex menu. It should be compatible with further smarty versions. I came up with the same idea, to "flatten" an array after reading the first page of this thread, so I made a powerful plugin to cover most - well, all I could think of - cases. _________________ Thinking is the hardest work there is, which is probably the reason why so few engage in it. (Henry Ford) |
|
Back to top |
|
lacop Smarty n00b
Joined: 01 Nov 2007 Posts: 2
|
Posted: Thu Nov 01, 2007 9:54 am Post subject: |
|
|
hi,
i want to display a category tree so i need some recursive function
i've tried to use the code on the first page of this thread (with some small changes) but it doesn't work
the error message is:
Fatal error: Using $this when not in object context in ...%%45^45E^45E480CD%%index.tpl.php on line 11
(i'm using php5)
line 11 is the line generated by the plugin, it looks like this:
Code: | <?php if (! function_exists('smarty_function_printtree')) {function smarty_function_printtree (&$this, $params) {$_fun_tpl_vars = $this -> _tpl_vars; $this -> assign ($params); ?> |
does anybody know how to fix it?
complete code:
compiler.define_function.php: rafb[dot]net/p/t8M9aT17.html
index.tpl: rafb[dot]net/p/Diifja54.html
(sorry for the [dot] but this is my first post so i can't post any url)
thanks for any help |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
|
Back to top |
|
lacop Smarty n00b
Joined: 01 Nov 2007 Posts: 2
|
Posted: Fri Nov 02, 2007 9:06 am Post subject: |
|
|
thanks |
|
Back to top |
|
rigo Smarty n00b
Joined: 29 Nov 2007 Posts: 3
|
Posted: Mon Jan 14, 2008 4:01 pm Post subject: |
|
|
Hi,
I've done a inclusion like this:
Code: | {defun name=ad_default file="listview/ad_default.htm" list=$adslist}
{foreach from=$list item=item key=key}
{fun name=ad_default item=$item key=$key}
{*include file="listview/ad_default.htm"*}
{/foreach}
{/defun}
{* Where $adslist is a list of 20 ads *} | but it doesn't work
I got an error:
Quote: | Fatal error: Maximum function nesting level of '100' reached, aborting!
...
10 5,2206 1874584 smarty_fun_ad_default( object(pnRender)[4], array(2) ) ..(null):57
11 5,2209 1875288 smarty_fun_ad_default( object(pnRender)[4], array(2) ) ..(null):55
12 5,2211 1876264 smarty_fun_ad_default( object(pnRender)[4], array(2) ) ..(null):55
13 5,2213 1878680 smarty_fun_ad_default( object(pnRender)[4], array(2) ) ..(null):55
14 5,2215 1881480 smarty_fun_ad_default( object(pnRender)[4], array(2) ) ..(null):55
15 5,2218 1884040 smarty_fun_ad_default( object(pnRender)[4], array(2) ) ..(null):55
... |
Could anyone give me a hint how to solve this?
[Edit] I'm using PHP 5.2.4
cu
rigo |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Jan 14, 2008 8:05 pm Post subject: |
|
|
I think you called
{fun name=ad_default item=$item key=$key}
with the wrong parameters as ad_default requires file and list as parameter. |
|
Back to top |
|
rigo Smarty n00b
Joined: 29 Nov 2007 Posts: 3
|
Posted: Mon Jan 14, 2008 9:13 pm Post subject: |
|
|
U.Tews wrote: | I think you called
{fun name=ad_default item=$item key=$key}
with the wrong parameters as ad_default requires file and list as parameter. | Ok. Could you give me an example how to use it in the right way with the "listview/ad_default.htm"-template file? |
|
Back to top |
|
|