View previous topic :: View next topic |
Author |
Message |
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Mar 01, 2006 6:19 pm Post subject: |
|
|
majglow wrote: | messju wrote: | I updated the pluging at http://lammfellpuschen.de/compiler.defun/ and added a more robust implementation of removing the trailing '<?php '. this version should work around the tokenizer bug.
please try that and tell me if it works for you. |
This link isn't working anymore. What is the latest status of this function? I am on PHP5 & the latest smarty. |
Yes it is. Messju's site was probably down when you tried. Try again |
|
Back to top |
|
yusufdestina Smarty Rookie
Joined: 07 Apr 2005 Posts: 23
|
Posted: Sat May 13, 2006 12:58 am Post subject: |
|
|
Fatal error: Using $this when not in object context in C:...compiler.defun.php on line 21
How can I fix this error? I'm using php5 and the latest smarty. tnx! |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat May 13, 2006 1:56 pm Post subject: |
|
|
yusufdestina: don't require the file explicitly, let smarty autoload it. then it should be required in a class context and the $this in line 21 will be valid. |
|
Back to top |
|
brettz9 Smarty Regular
Joined: 07 Jul 2006 Posts: 93
|
Posted: Fri Jul 07, 2006 6:35 am Post subject: |
|
|
Hi,
I have built a multdimensional array ($tree) for which this function you have developed seems ideal.
I have added the compiler.defun.php file to /libs/plugins/
But while trying the following script as a test (drawing from boots' template allowing tracking of levels supplied earlier), I get an internal server error (unless I take out the "fun" call).
{defun name="menu" list=$tree level=0}
{assign var=level value=$level+1}
<div style="padding-left:{$level*2};">my data</div>
{fun name="menu" list=$i level=$level}
{/defun}
Do you have any idea what's going on?
thanks much in advance,
Brett
p.s. I'm working with PHP 4.4.1 and Smarty 2.6.14 (and other templates not calling this function are ok)
p.s.s. If you need to see the array data, it is listed in the middle of the page at http://bahai-library.com/browser/test-browse-18/recurs.php |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Jul 07, 2006 2:57 pm Post subject: |
|
|
Don't forget to bail out of the recursion. You have to test $list (btw: where is $i coming from?) to see if it is an array. If it isn't, you shouldn't be recursing back into the menu function. |
|
Back to top |
|
brettz9 Smarty Regular
Joined: 07 Jul 2006 Posts: 93
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Jul 08, 2006 7:22 am Post subject: |
|
|
@brettz9: recursion (especially in Smarty) is at least an intermediate technique -- not suited for cut & paste. Try reading the thread over again and see what you come up with |
|
Back to top |
|
brettz9 Smarty Regular
Joined: 07 Jul 2006 Posts: 93
|
Posted: Sat Jul 08, 2006 7:44 am Post subject: |
|
|
Yeah, I know.
I actually figured it out now. I just thought in the beginning that this was something to automatically handle a multi-dimensional array till its end, and didn't want to try to unravel the coding of the plugin itself before I tested to make sure a simple run would work. Now that I have looked over the posts more carefully, successfully tested it, and understand it, I see it is not necessary to understand the internals of the plugin.
Thank you guys by the way so much for coming up with this (including your level tracking addition). After I had developed the array (and had recently switched to Smarty), I was despairing that Smarty might not be sophisticated enough to handle this kind of data. I guess I was wrong. It's especially cool that it can take any number of arguments...
By the way, if you ever package this standardly, I put in my vote for your {macro} and {macro_call} names (or what about {recurs} and {recurs_call} if somebody didn't suggest that already).
take care,
Brett |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Jul 08, 2006 8:30 am Post subject: |
|
|
brettz9 wrote: | {macro} and {macro_call} names |
Or even, {func} and {call} ... but I think that maybe messju is reveling in his LISPy glory |
|
Back to top |
|
smak Smarty Rookie
Joined: 15 Jun 2006 Posts: 5
|
Posted: Tue Jul 11, 2006 1:19 pm Post subject: Assigning variables |
|
|
Variables assigned within a {defun} aren't asigned to the whole template.
This won't work for example:
[php:1:b4385c7827]<ul>
{defun name="tree" list=$tree lists=1}
{foreach from=$list item=element}
<li>
{$element.name}
{if $element.element}
{assign var=lists value=$lists+1}
<ul>{fun name="tree" list=$element.element lists=$lists}</ul>
{/if}
</li>
{/foreach}
{/defun}
</ul>
Number of lists: {$lists}[/php:1:b4385c7827]
Is there a workaround for this? Because I need to know the number of lists. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jul 11, 2006 3:17 pm Post subject: Re: Assigning variables |
|
|
smak wrote: | Is there a workaround for this? Because I need to know the number of lists. |
Well, maybe consider writing a proper plugin. I don't think that these template functions were ever intended to return anything or have side-effects other than outputing some text.
...however, as a hacky, hacky workaround, you can define a parameter that only returns your value of interest:
Code: | {defun name=foo lists=1 return_data=false}
{if $return_data}
{$lists}
{else}
foo
{/if}
{/defun}
{fun name=foo}
{fun name=foo return_data=true}
|
yuck. |
|
Back to top |
|
brettz9 Smarty Regular
Joined: 07 Jul 2006 Posts: 93
|
Posted: Tue Jul 11, 2006 5:44 pm Post subject: |
|
|
I think this is ok, no? At least it can be done...
[php:1:0c2a5a8ed0]<ul>
{defun name="tree" list=$tree lists=1}
{foreach from=$list item=element}
<li>
{$element.name}
{if $element.element}
{assign var=lists value=$lists+1}
<ul>{fun name="tree" list=$element.element lists=$lists}</ul>
{/if}
</li>
{/foreach}
{capture name=num_lists}
{$lists}
{/capture}
{/defun}
</ul>
Number of lists: {$smarty.capture.num_lists}[/php:1:0c2a5a8ed0] |
|
Back to top |
|
m-lee Smarty Rookie
Joined: 17 Mar 2004 Posts: 31
|
Posted: Tue Jul 25, 2006 3:16 pm Post subject: problem with an index / id |
|
|
First of, great plugin, thanks!
All works as accepted but im still have a little problem.
Im looping throug a directory structure and need to set an unique numeric autoincrement index for each file.
Is there any way to do it? I failed so far by trying to pass variables and using section iteration.
The node_id must be unique.
Thank you for your help.
Regards m-lee
[php:1:ef7e2afbd2]
{defun name="dirmenu" list=$ARRAY_DIRS level=0 node_id=$nodie_id previous_node_id=$previous_node_id}
{assign var=level value=$level+1}
{foreach from=$list item=o_menu name=co}
<li>
{assign var=node_id value=$node_id+1}
{if $o_menu.name != "system_cache" && $o_menu.name != "system_compile"}
node_id: {$node_id}
previous_node_id: {$previous_node_id}
{if $o_menu.kind == "file"}
<a href="javascript:SelectFile('{$o_menu.name}');">{$o_menu.name}</a>
{else}
{$o_menu.name}
{/if}
{/if}
{if $o_menu.content && $level <=3}
<ul>{fun name="dirmenu" list=$o_menu.content level=$level node_id=$node_id previous_node_id=$node_id}</ul>
{/if}
</li>
{/foreach}
{/defun} [/php:1:ef7e2afbd2] |
|
Back to top |
|
restak Smarty Rookie
Joined: 10 Aug 2006 Posts: 14
|
Posted: Fri Aug 11, 2006 9:29 am Post subject: |
|
|
I have a serious problem with the defun-plugin if the template is created out of a function. see here |
|
Back to top |
|
Adar Smarty Regular
Joined: 27 May 2004 Posts: 58
|
Posted: Sun Sep 24, 2006 10:11 pm Post subject: |
|
|
Many many thanks for this great work !
I dont know exactly how it works...but it does
and nice fast |
|
Back to top |
|
|