View previous topic :: View next topic |
Author |
Message |
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Apr 24, 2009 10:49 pm Post subject: new {function} tag allows recursion |
|
|
Smarty 3 has now a new {function...} tag. It allows to define a "function" within a template.
This allows to reuse code sequences and the best of it is that it can all itself recursively.
See the following example for e nested menu ouput.
Template file:
Code: |
{function name=menu level=0}
<ul class="level{$level}">
{foreach $data as $entry}
{if is_array($entry)}
<li>{$entry@key}</li>
{menu data=$entry level=$level+1}
{else}
<li>{$entry}</li>
{/if}
{/foreach}
</ul>
{/function}
{$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' => ['item3-3-1','item3-3-2']],'item4']}
{menu data=$menu}
|
Generated output:
* item1
* item2
* item3
____o item3-1
____o item3-2
____o item3-3
_______+ item3-3-1
_______+ item3-3-2
* item4
The function tag itself must have the name attribute. This name will become the tag name when calling the function. The function tag may have any number of additional attributes. These will be default settings for local variables.
When the function is called by its name the attributes will become local variables eventually overwriting the defaults. |
|
Back to top |
|
jLix Smarty Regular
Joined: 01 Apr 2009 Posts: 59 Location: Lowlands, EU
|
Posted: Sat Apr 25, 2009 12:03 pm Post subject: |
|
|
Is it possible to overwrite existing functions, like {function name="foreach"}...{/function}? _________________ http://jlix.net/extensions/smarty |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Apr 25, 2009 12:43 pm Post subject: |
|
|
Yes, it is possible to overwrite the standard Smarty tags.
I forgot to mention that the scope of the function is local. It can only be called within the template which defines it. |
|
Back to top |
|
jLix Smarty Regular
Joined: 01 Apr 2009 Posts: 59 Location: Lowlands, EU
|
Posted: Sat Apr 25, 2009 1:27 pm Post subject: |
|
|
U.Tews wrote: | I forgot to mention that the scope of the function is local. It can only be called within the template which defines it. |
... and all templates that are included from within that template I guess. Or not? _________________ http://jlix.net/extensions/smarty |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Apr 26, 2009 4:40 am Post subject: |
|
|
No, currently the function is only known within the template in which it was defined. |
|
Back to top |
|
jLix Smarty Regular
Joined: 01 Apr 2009 Posts: 59 Location: Lowlands, EU
|
Posted: Sun Apr 26, 2009 11:48 am Post subject: |
|
|
Would it be possible to let included templates inherit the function, or make that optional through a parameter? _________________ http://jlix.net/extensions/smarty |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Mon Apr 27, 2009 12:20 am Post subject: |
|
|
this is nice! i was just thinking about something like this...
jLix wrote: | Would it be possible to let included templates inherit the function, or make that optional through a parameter? |
yea that would be good.
templates included after the function is defined, edit: on second thought, this one may not as much sense.
or, if a template includes the file it could see the function.
since you can override smarty tags, could this cause any type of security issue? And why would one want to override a smarty tag?
Last edited by douglassdavis on Tue Apr 28, 2009 3:45 pm; edited 2 times in total |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Tue Apr 28, 2009 11:44 am Post subject: |
|
|
since there was no reply, i would like to say I am against being able to override smarty tags. changing the way, for example, a foreach works just seems like it would lead to trouble. And you can always create a function with a different name that does what you want.
But, what I would like to see is the ability to create a globally defined function available in the plugins directory.
It would just be a .tpl file, and work just like the php files work now.
That should be pretty easy to implement since you've already got the functions.
And since the whole purpose of a function is re-use... why be limited to reuse in one file? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Apr 28, 2009 12:02 pm Post subject: |
|
|
Thank's for all of your input.
I will look into the requested features during the next couple of days and keep you updated.
Uwe |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Apr 28, 2009 3:44 pm Post subject: |
|
|
With todays update of the SVN the {function} tag no longer can overwrite standard Smarty tags.
Also functions defined with the {function} tag are now available in subtemplates.
Uwe |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Apr 30, 2009 5:54 pm Post subject: |
|
|
The scope of functions defined with the {fuction} tag is now always global. The fuction can be called from any template after it has been defined.
For reuse of functions you can create template files which just contain the function definitions. They can be included to make the functions available.
Uwe |
|
Back to top |
|
jLix Smarty Regular
Joined: 01 Apr 2009 Posts: 59 Location: Lowlands, EU
|
Posted: Sun May 03, 2009 4:00 pm Post subject: |
|
|
U.Tews wrote: | With todays update of the SVN the {function} tag no longer can overwrite standard Smarty tags. |
U.Tews wrote: | The scope of functions defined with the {fuction} tag is now always global. The fuction can be called from any template after it has been defined. |
How about plugin functions then?
Say I'm working on a project in which I include my {json} plugin and later on, in the same project, another front-end engineer defines a json function of his own using {function}, which works slightly different. Will this corrupt the {json} function defined by the plugin?
If so, wouldn't is be better to use a standard prefix for functions that are defined with {function}, for instance using an underscore:
Code: | {function name="fn"}...{/function}
...
{_fn} |
Some other prefix suggestions:But any prefix that's not likely to be the beginning of a current plugin function will do.
Spam: the aforementioned {json} plugin can be found at http://jlix.net/extensions/smarty/json _________________ http://jlix.net/extensions/smarty |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun May 03, 2009 4:56 pm Post subject: |
|
|
Functions defined with the {function} are compiled and stored in the compiled template file. They do not currupt plugins.
When I said that they have global scope it means during the lifetime of the script.
If you have a plugin with same name the {function} definition has higher priority. |
|
Back to top |
|
nielsNL Smarty Rookie
Joined: 03 May 2009 Posts: 17
|
Posted: Sun May 03, 2009 8:40 pm Post subject: |
|
|
Hello,
I'm watching the growing of smarty 3, with some interest. The project i'm working on (www.fusionticket.nl) uses smarty 2 for displaying the content. And i love the simplicity of it. And want to use Smarty 3 when we ready with our own redisgning of that project.
Somehow i think this option is very nice. But i don't want any programming into the templates (also no php in it). It will be nice if it would be posible disable the {function}?
Thanks and keep on working |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun May 03, 2009 10:11 pm Post subject: |
|
|
I don't see the need for having he option to disable the {function} tag as you could still can use all the other Smarty tag like {if}, {foreach} etc. to "program" anyway. You can see {function} also as a marco to reuse any type of template content.
Regards Uwe |
|
Back to top |
|
|