Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Anonymous functions with Smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Simmons199
Smarty n00b


Joined: 14 Aug 2011
Posts: 2

PostPosted: Sun Aug 14, 2011 10:10 pm    Post subject: Anonymous functions with Smarty Reply with quote

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


Joined: 10 Jan 2011
Posts: 96

PostPosted: Mon Aug 15, 2011 5:45 am    Post subject: Reply with quote

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
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Mon Aug 15, 2011 7:44 am    Post subject: Reply with quote

Code:
$smarty = new Smarty();
$func = function($foo){ return $foo . " world"; };
$smarty->assign('func', $func);
$smarty->display("anonymous.tpl");
and
Code:
{$func("hello")}
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
Code:
{func foo="hello"}
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
View user's profile Send private message Visit poster's website
Simmons199
Smarty n00b


Joined: 14 Aug 2011
Posts: 2

PostPosted: Mon Aug 15, 2011 10:37 am    Post subject: Reply with quote

Too bad.

Anyway thanks for you replies.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Aug 15, 2011 1:51 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Mon Aug 15, 2011 2:00 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Aug 15, 2011 2:03 pm    Post subject: Reply with quote

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


Joined: 10 Jan 2011
Posts: 96

PostPosted: Tue Aug 16, 2011 6:23 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
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