Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

return value for {function} or define global variables

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
lauff
Smarty n00b


Joined: 21 Oct 2010
Posts: 1

PostPosted: Thu Oct 21, 2010 7:54 am    Post subject: return value for {function} or define global variables Reply with quote

Hi,

I would need values calclulated by a {function}.

Do you support return values?

As an alternative, it would be also good enough to define a global variable within the {function}.

Thanks for the great work,
Markus
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Oct 21, 2010 1:36 pm    Post subject: Reply with quote

{function} return value is echoed to the page. You can implement an assign param:

{function ... assign="foo"}

See other plugins for examples.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Thu Oct 21, 2010 1:38 pm    Post subject: Reply with quote

Use the $smarty->assign(....); inside the plugin function to assign the result to a Smarty variable instead of returning it.
Back to top
View user's profile Send private message
andrewFountain
Smarty n00b


Joined: 31 Aug 2018
Posts: 1

PostPosted: Fri Aug 31, 2018 9:21 am    Post subject: For future me Reply with quote

You can surround a function call with a capture to assign the output of the function to a variable in your template scope.

Code:

{function generateText}
  function text
{/function}

{capture getText assign=text}
  {generateText}
{/capture}

{$text}


If the output is needed for comparison the code blocks should have white space removed using {strip} tags or having no new lines in either the function or capture block

Code:

{function generateText}function text{/function}

{capture getText assign=text}{generateText}{/capture}

{$text}
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Aug 31, 2018 9:39 am    Post subject: Reply with quote

Template functions can return values by assigning variables in parent scope

Code:
{function somefunction}
......
  {assign $retvalue = $foo scope=parent}
  {assign $otherretvalue = $bar scope=parent}

{/function}


Code:
{call somefunction}
{$retvalue}
{$otherretvalue}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 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