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 ... , 9, 10, 11  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Olin
Smarty n00b


Joined: 23 Oct 2008
Posts: 4

PostPosted: Thu Oct 23, 2008 5:18 pm    Post subject: Reply with quote

very usefool thanks a lot !!!
Back to top
View user's profile Send private message
Siskohh
Smarty n00b


Joined: 26 Oct 2008
Posts: 3

PostPosted: Sun Oct 26, 2008 6:03 am    Post subject: Reply with quote

This is the the structure of my array:

Code:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [name] => vati
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [parent] => 1
                            [name] => sohn1
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 4
                            [parent] => 1
                            [name] => sohn2
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [parent] => 0
            [name] => mutti
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [parent] => 2
                            [name] => tochter1
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 6
                            [parent] => 2
                            [name] => tochter2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [parent] => 6
                                            [name] => tochtertochter
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)



How would I go about using this plugin to format that array to something like this:

-> vati
----> sohn1
----> sohn2
-> mutti
----> tochter1
----> tochter2
---------> tochtertochter


And even farther, sub childs within sub childs... etc.



I'd tried:

<ul>
<% defun name="testrecursion" list=$Sitemap %>
<% foreach from=$data2 item=element %>
<li><% $element.id %>
<% if $element.element %>
<ul><% fun name="testrecursion" list=$element.element %></ul>
<% /if %>
</li>
<% /foreach %>
<% /defun %>
</ul>


Bt that just gives me forums 1 and 4 and no sub forums.

What am I doing wrong?
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Sun Oct 26, 2008 6:06 am    Post subject: Reply with quote

you should foreach from=$list not from=$data2 (untested),
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Siskohh
Smarty n00b


Joined: 26 Oct 2008
Posts: 3

PostPosted: Sun Oct 26, 2008 6:12 am    Post subject: Reply with quote

you are right messju, i've changed it. but it doesn't fixed my problem..

Code:

<div style="background-color: #FFCC99">
   Test:<br />
   <ul>
      {defun name="testrecursion" list=$Sitemap}
         {foreach from=$list item=element}
            <li>{$element.name}</li>
               {if $element.element}
                  <ul>{fun name="testrecursion" list=$element.element}</ul>
               {/if}
         {/foreach}
      {/defun}
   </ul>
</div>


is giving me out this:

Code:

<div style="background-color: rgb(255, 204, 153);">
   Test:<br>
   <ul>
      <li>vati</li>
      <li>mutti</li>
   </ul>
</div>


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


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

PostPosted: Sun Oct 26, 2008 8:26 am    Post subject: Reply with quote

And you have to recurse into $element.children not $element.element, obviously.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Siskohh
Smarty n00b


Joined: 26 Oct 2008
Posts: 3

PostPosted: Sun Oct 26, 2008 9:49 pm    Post subject: Reply with quote

you are the best Smile it has geklappt!
Back to top
View user's profile Send private message
kalleplom
Smarty Rookie


Joined: 23 Sep 2006
Posts: 6
Location: berlin

PostPosted: Mon Jan 25, 2010 9:51 am    Post subject: Reply with quote

This is just the article i was looking for, but i got stuck...:

I copied the codes into the named file and left it all like it is. When trying to compile the HTML is stops with the following:

Code:
Fatal error: Using $this when not in object context in ....\%%30^301^3018A601%%sectionlist.tpl.php on line 48



i'm using smarty 2.6.22 and am not that good in programming smarty core functions.. as you see .. Embarassed

thank you so much for your help and this great contribution!
Back to top
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 69

PostPosted: Tue Jan 26, 2010 11:10 pm    Post subject: Reply with quote

This will be a PHP error and not a smarty one, ensure you dont have any $this->whatevers() outside of a class.
Back to top
View user's profile Send private message
kalleplom
Smarty Rookie


Joined: 23 Sep 2006
Posts: 6
Location: berlin

PostPosted: Wed Jan 27, 2010 6:36 am    Post subject: Reply with quote

hi!

Yes, you're right. This is a php-error, of course. It is in the compiled php file smarty generated.
So there must be an error in the script above, or something doesn't match with the smarty version i'm using.

Code:
<?php if (!function_exists('smarty_fun_testrecursion')) {
      function smarty_fun_testrecursion(&$this, $params) {
         $_fun_tpl_vars = $this->_tpl_vars;
         $this->assign($params);  ?>
<?php $_from = $this->_tpl_vars['list']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }if (count($_from)):
    foreach ($_from as $this->_tpl_vars['element']):
?>


The first error appears in $_fun_tpl_vars = $this->_tpl_vars;
Because you can't use $this in the function scope. When I change the arguments function smarty_fun_testrecursion(&$this, $params) to
function smarty_fun_testrecursion(&$smarty, $params) and all calls to $smarty->... in the compiled file it works fine.

But what to change in the smarty plug-in?
Back to top
View user's profile Send private message
alex
Smarty Rookie


Joined: 03 Sep 2005
Posts: 19
Location: Argentina

PostPosted: Mon Feb 15, 2010 9:56 am    Post subject: compiler.defun.php and Smarty 3 Reply with quote

Hi,

does anybody know if it's possible to use the compiler.defun plugin with Smarty 3, and if so, what modifications need to be done?

In a first test with Smrty 3 I got the following error message:

Fatal error: Call to undefined method Smarty_Internal_SmartyTemplateCompiler::_parse_attrs() in /path/to/my/application/smarty/plugins/compiler.defun.php on line 26

So it seems like the _parse_attrs method isn't available anymore in Smarty 3. Any ideas?

Thanks,
Alex
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Mon Feb 15, 2010 4:22 pm    Post subject: Reply with quote

Plugins which do make use of Smarty2 internal functions will not run without modification in Smarty3.

However Smarty3 does provide the functionallity of the defun compiler plugin by the {function...} tag. Please see the README.
Back to top
View user's profile Send private message
alex
Smarty Rookie


Joined: 03 Sep 2005
Posts: 19
Location: Argentina

PostPosted: Mon Feb 15, 2010 10:14 pm    Post subject: Reply with quote

U.Tews wrote:
However Smarty3 does provide the functionallity of the defun compiler plugin by the {function...} tag. Please see the README.


That's interesting, thank you!
Back to top
View user's profile Send private message Visit poster's website
alex
Smarty Rookie


Joined: 03 Sep 2005
Posts: 19
Location: Argentina

PostPosted: Tue Feb 16, 2010 10:42 am    Post subject: Reply with quote

Hi again,

I tested the new {function}...{/function} block tag and it worked fine. However it seems to be slower than the defun compiler plugin.

I use it for the tree structured threads of this web forum. With Smarty 2.6.26 and the compiler defun plugin the elapse time of the index page is about 0.03 s (local webserver). Smarty 3.0 Beta 7: about 0.2 s.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Tue Feb 16, 2010 9:17 pm    Post subject: Reply with quote

Do you have force_compile=false?

The Smarty3 compiling process is slower than in Smarty2 but once the templates are compiled the rendering times should be almost the same.
Back to top
View user's profile Send private message
alex
Smarty Rookie


Joined: 03 Sep 2005
Posts: 19
Location: Argentina

PostPosted: Wed Feb 17, 2010 10:12 am    Post subject: Reply with quote

U.Tews wrote:
Do you have force_compile=false?


I didn't set force_compile so it should be false (default).

Here is a little demonstration script:
http://www.mediafire.com/?emvmfrwman2

You can switch between Smarty 2 and 3 and the elapse time is displayed at the bottom of the page. The folders templates_c_2 and templates_c_3 need to be writable.
Back to top
View user's profile Send private message Visit poster's website
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 ... , 9, 10, 11  Next
Page 10 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