View previous topic :: View next topic |
Author |
Message |
Simmons199 Smarty n00b
Joined: 14 Aug 2011 Posts: 2
|
Posted: Sun Aug 14, 2011 10:10 pm Post subject: Anonymous functions with Smarty |
|
|
Hi,
is there any way to use anonymous functions within a smarty template?
Working example in PHP:
Code: | $myFunction = function($foo) { return $foo . 'bar'; };
echo $myFunction('foo');
|
I tried to transfer the code to smarty, this way:
PHP:
Code: | $myFunction = function($foo) { return $foo . 'bar'; };
$smarty->assign('myFunction',$myFunction);
|
Smarty:
Code: | {$myFunction('foo')}
|
The attempt ended with a syntax error.
Any suggestions?
Thanks |
|
Back to top |
|
Aristophan Smarty Regular
Joined: 10 Jan 2011 Posts: 96
|
Posted: Mon Aug 15, 2011 5:45 am Post subject: |
|
|
Code: | function($foo) { return $foo . 'bar'; };
$smarty->register_function('isfoo', 'foo'); | in Smarty2
or
Code: | $smarty->registerPlugin('function', 'isfoo', 'foo'); | in Smarty3
use {isfoo} in template |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Mon Aug 15, 2011 7:44 am Post subject: |
|
|
Code: | $smarty = new Smarty();
$func = function($foo){ return $foo . " world"; };
$smarty->assign('func', $func);
$smarty->display("anonymous.tpl"); | and currently lead to a compiler exception.
Code: | $smarty = new Smarty();
$func = function($params, $template){ return $params['foo'] . " world"; };
$smarty->registerPlugin('function', 'func', $func);
$smarty->display("anonymous.tpl"); | and currently lead to a fatal error, as smarty does not expect a callable thrown into registerPlugin().
So no, closures / lambdas / anonymous functions are not usable within a Smarty template at the moment. I'm not sure I like the general idea, though. _________________ Twitter |
|
Back to top |
|
Simmons199 Smarty n00b
Joined: 14 Aug 2011 Posts: 2
|
Posted: Mon Aug 15, 2011 10:37 am Post subject: |
|
|
Too bad.
Anyway thanks for you replies. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Aug 15, 2011 1:51 pm Post subject: |
|
|
There are plenty of ways around it, such as a plugin wrapper:
{anon funcname="foo"}
I agree with globe, I'm not sure of the usefulness of this one. |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Mon Aug 15, 2011 2:00 pm Post subject: |
|
|
mohrt wrote: | I agree with globe, I'm not sure of the usefulness of this one. |
I'm not saying it couldn't be useful. I'm saying it's effing confusing for template developers (not knowing php). Anonymous functions would add another level of complexity to smarty templates, making them even harder to understand by non-programmers. _________________ Twitter |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Aug 15, 2011 2:03 pm Post subject: |
|
|
globe wrote: | mohrt wrote: | I agree with globe, I'm not sure of the usefulness of this one. |
I'm not saying it couldn't be useful. I'm saying it's effing confusing for template developers (not knowing php). Anonymous functions would add another level of complexity to smarty templates, making them even harder to understand by non-programmers. |
I don't think it would even be useful, ie. there would always be a better way. |
|
Back to top |
|
Aristophan Smarty Regular
Joined: 10 Jan 2011 Posts: 96
|
Posted: Tue Aug 16, 2011 6:23 am Post subject: |
|
|
oops, I should have read better. Sorry for this.
btw, what is the use for an anonymous function called by varname? I can understand its shortened use in callbacks and equal.
Why should you want this working example to be an anonymous function?
Code: | function foo($params, $template) { return $params['foo'] . " world"; }
$smarty->registerPlugin('function', 'isfoo', 'foo');
{isfoo foo="hello"} |
|
|
Back to top |
|
|