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

How cna I clear all variables assigned in a template?

 
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
redAppleCore
Smarty n00b


Joined: 01 Aug 2013
Posts: 3

PostPosted: Fri Oct 10, 2014 2:22 pm    Post subject: How cna I clear all variables assigned in a template? Reply with quote

Is there a way to clear variables assigned inside of a template? I tried clearAllAssign but it seems like they must be stored somewhere else. Here's what I tried


Code:

$objSmarty = new Smarty();

echo $objSmarty->fetch("string:{assign var='TemporaryVariable' value='foo'}Assigned Temporary Variable\n");
$objSmarty->clearAllAssign();
echo $objSmarty->fetch('string:OutputtingTemporary Variable: {$TemporaryVariable}');


I figure I can re-initialize Smarty, but it's in a time sensitive area where it would happen hundreds of times, is there a better way?[/code]
Back to top
View user's profile Send private message
redAppleCore
Smarty n00b


Joined: 01 Aug 2013
Posts: 3

PostPosted: Fri Oct 10, 2014 4:18 pm    Post subject: Reply with quote

It looks like it doesn't happen in the current version. Seems we may also have made some edits that could be responsible, so may not have ever happened.

*edit* definitely our fault.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Oct 10, 2014 5:42 pm    Post subject: Reply with quote

Note: Variales assigned within a template like your TemporaryVariable have normally a local scope and not see in the outside world. So it's not seen in the Smarty object and is also not know at your second fetch.

To make it know also to the outside world you must assign it to the root scope.
Code:
{assign var='TemporaryVariable' value='foo' scope=root}


$smarty->clearAllAssign() does clear only variables assigned to the Smarty object (root scope)
Back to top
View user's profile Send private message
rudder
Smarty Rookie


Joined: 20 Oct 2009
Posts: 14

PostPosted: Tue Mar 29, 2022 4:39 am    Post subject: Reply with quote

Sorry for bringing such an old post back to life, but this is the most relevant discussion to what we're experiencing.

We have a template like this:

Code:

//tpl test.tpl
{$var="$value" scope="global"}


And we have PHP code like this:

Code:

foreach ($array as $i => $v) {
   $smarty->clearAllAssign();
   $smarty->assign("value","$i");
   $smarty->fetch("test.tpl");
}


In the above example, I don't think clearAllAssign is properly clearing the template variable "$var" at the beginning of each loop. Is there a way to clear all variables that are assigned inside the template, with global scope?[/code]
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Apr 06, 2022 11:22 am    Post subject: Reply with quote

There's a way to not do that.
If you are pre-rendering your website and need to render the same template with slightly different input data, use Smarty::createTemplate method.

Code:
foreach ($array as $i => $v) {
   $smarty->createTemplate("test.tpl", ["value" =>"$i"])->fetch();
}
Back to top
View user's profile Send private message
rudder
Smarty Rookie


Joined: 20 Oct 2009
Posts: 14

PostPosted: Wed Apr 06, 2022 6:03 pm    Post subject: Reply with quote

We ended up changing to
Code:
scope='root'
which did is probably a good enough solution, as long as the template variables don't have a conflict with template variables assigned in the PHP.

So the code becomes:

Code:

//tpl test.tpl
{$var="$value" scope="root"}


And we have PHP code like this:

Code:

foreach ($array as $i => $v) {
   $smarty->clearAllAssign();
   $smarty->assign("value","$i");
   $smarty->fetch("test.tpl");
}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Apr 10, 2022 9:18 pm    Post subject: Reply with quote

That's a very bad idea.
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