|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
lauff Smarty n00b
Joined: 21 Oct 2010 Posts: 1
|
Posted: Thu Oct 21, 2010 7:54 am Post subject: return value for {function} or define global variables |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Oct 21, 2010 1:36 pm Post subject: |
|
|
{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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Oct 21, 2010 1:38 pm Post subject: |
|
|
Use the $smarty->assign(....); inside the plugin function to assign the result to a Smarty variable instead of returning it. |
|
Back to top |
|
andrewFountain Smarty n00b
Joined: 31 Aug 2018 Posts: 3
|
Posted: Fri Aug 31, 2018 9:21 am Post subject: For future me |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Aug 31, 2018 9:39 am Post subject: |
|
|
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 |
|
|
|
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
|