Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
ADVANCED: Recursion with Smarty
Goto page Previous  1, 2, 3 ... 8, 9, 10, 11  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
boots
Administrator


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

PostPosted: Mon Sep 25, 2006 8:02 am    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Mon Sep 25, 2006 9:49 am    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message Visit poster's website
muhv
Smarty n00b


Joined: 07 May 2003
Posts: 3

PostPosted: Tue Nov 28, 2006 5:55 am    Post subject: Reply with quote

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 Sad Is there some other solution to make thos last ending tree elements???? (like Menu G & Menu K )

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


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

PostPosted: Tue Nov 28, 2006 4:07 pm    Post subject: Reply with quote

muhv wrote:
I cant determine last item in submenu. Looks like smarty doesnt remember loop name variable after reqursion Sad 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
View user's profile Send private message
stot
Smarty Rookie


Joined: 13 Nov 2006
Posts: 17

PostPosted: Tue Dec 05, 2006 9:30 am    Post subject: Re: Recursion for HTML List Reply with quote

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
View user's profile Send private message Visit poster's website
A320
Smarty n00b


Joined: 04 Mar 2007
Posts: 2

PostPosted: Sun Mar 04, 2007 6:40 am    Post subject: Reply with quote

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
View user's profile Send private message
Vicky`s Webgeek
Smarty Rookie


Joined: 07 Mar 2007
Posts: 9

PostPosted: Wed Mar 07, 2007 3:42 pm    Post subject: Reply with quote

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
View user's profile Send private message
pvginkel
Smarty n00b


Joined: 04 Aug 2006
Posts: 3

PostPosted: Wed May 23, 2007 4:00 pm    Post subject: Reply with quote

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 Sad 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
View user's profile Send private message
HZiegler
Smarty Rookie


Joined: 04 Aug 2007
Posts: 5
Location: Germany

PostPosted: Wed Aug 29, 2007 4:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
lacop
Smarty n00b


Joined: 01 Nov 2007
Posts: 2

PostPosted: Thu Nov 01, 2007 9:54 am    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Thu Nov 01, 2007 12:20 pm    Post subject: Reply with quote

the version at http://lammfellpuschen.de/compiler.defun/ should work with php4 and 5. you just have to recompile all templates (read: clear templates_c.)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
lacop
Smarty n00b


Joined: 01 Nov 2007
Posts: 2

PostPosted: Fri Nov 02, 2007 9:06 am    Post subject: Reply with quote

thanks Smile
Back to top
View user's profile Send private message
rigo
Smarty n00b


Joined: 29 Nov 2007
Posts: 3

PostPosted: Mon Jan 14, 2008 4:01 pm    Post subject: Reply with quote

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 Sad
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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4625
Location: Hamburg / Germany

PostPosted: Mon Jan 14, 2008 8:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
rigo
Smarty n00b


Joined: 29 Nov 2007
Posts: 3

PostPosted: Mon Jan 14, 2008 9:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page Previous  1, 2, 3 ... 8, 9, 10, 11  Next
Page 9 of 11

 
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