View previous topic :: View next topic |
Author |
Message |
redAppleCore Smarty n00b
Joined: 01 Aug 2013 Posts: 3
|
Posted: Fri Oct 10, 2014 2:22 pm Post subject: How cna I clear all variables assigned in a template? |
|
|
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 |
|
redAppleCore Smarty n00b
Joined: 01 Aug 2013 Posts: 3
|
Posted: Fri Oct 10, 2014 4:18 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Oct 10, 2014 5:42 pm Post subject: |
|
|
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 |
|
rudder Smarty Rookie
Joined: 20 Oct 2009 Posts: 14
|
Posted: Tue Mar 29, 2022 4:39 am Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Apr 06, 2022 11:22 am Post subject: |
|
|
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 |
|
rudder Smarty Rookie
Joined: 20 Oct 2009 Posts: 14
|
Posted: Wed Apr 06, 2022 6:03 pm Post subject: |
|
|
We ended up changing to 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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Apr 10, 2022 9:18 pm Post subject: |
|
|
That's a very bad idea. |
|
Back to top |
|
|