|
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 |
calguy1000 Smarty Rookie
Joined: 27 Mar 2009 Posts: 25
|
Posted: Sat Feb 18, 2012 9:44 pm Post subject: Force all assigns to root scope. |
|
|
Smarty 2 did not have the concept of 'scopes'... and we built a whole CMS around that concept. i.e: you could assign a variable in a sub-template, or a plugin, or a resource handler, (wherever basically) and use it in the main template easily. As long as the variable was 'assigned' before it was used in the flow of the template. We created a singleton Smarty object, and all assigns were referenced to that object.
i.e: main.tpl
Code: |
{myplugin assign='foo3'}
{$foo1}{* should output bar1 *}
{$foo2}{* should output bar2 *}
{$foo3}{* should output bar1 *}
|
function.myplugin.php:
Code: |
function smarty_function_myplugin($params,&$smarty) {
$smarty->assign('foo1','bar1'};
$out = $smarty->fetch('myresource:myplugin_tpl');
if( isset($params['assign']) ) {
$smarty->assign($params['assign'],$out); return;
}
return $out;
}
|
myplugin_tpl (probably stored in database)
Code: |
{$foo1}
{assign var='foo2' value='bar2'}
|
of course, this plugin could possibly call other plugins that use different resources, etc. and create additional variables.
Smarty 3 of course, changed all of that. And although in principle I love the new variable scoping stuff, we have an existing CMS with tens of thousands of websites running with the older style so we cannot break backwards compatibility (completely) with a new version of the software.
So my question is.. how can I force assign() and {assign} and {capture} to always assign to the 'root' object, instead of to the local scope?
In a perfect world, I would like the scoping to be explicit, rather than implicit. i.e: if I called $obj1 = $smarty->createTemplate() or $obj2 = $smarty->createData and assigned variables to either of $obj1 or $obj2 then they would not be in the root scope... but absolutely everything else would. i.e: {assign var='foo' value='bar'} would always add a variable to the root scope. Also, in a perfect world this would not interfere with the function stuff, or template inheritance, or any of those other nice features of smarty3.
Using alternative functions for {assign} and {capture} and for $smarty->assign() is not an option due to backwards compatibility problems. However, if possible, I can override them. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Feb 19, 2012 12:07 pm Post subject: |
|
|
You have not observed one major change between Smarty 2 and Smarty 3.
Smarty 3 does no longer pass the Smarty object but the template object to plugins.
In your case you must indirectly access the Smarty object.
Code: | function smarty_function_myplugin($params, $template) {
$template->smarty->assign('foo1','bar1'};
$out = $template->smarty->fetch('myresource:myplugin_tpl');
if( isset($params['assign']) ) {
$template->smarty->assign($params['assign'],$out); return;
}
return $out;
}
|
For an assign from template you can use the scope attribute.
Code: | {assign var='foo2' value='bar2' scope=root} |
|
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Sun Feb 19, 2012 12:28 pm Post subject: |
|
|
While your last reply was totally correct, it did not address calguy1000's main concern, Uwe.
I believe what calguy1000 is looking for is a way to (from plugins) not only assign values to the current scope, but also have them assigned to the higher scopes.
If I'm not mistaking, this is pretty much what we've been talking about the other day. You already put the "bubbling assign()" on the ToDo for 3.2.
When we discussed this, we thought of the bubbling as an opt-in thing. Something you'd have to explicitly request when assigning data. How about making this "bubble or not" thing configurable through $smarty->bubble_variables = true;? _________________ Twitter |
|
Back to top |
|
calguy1000 Smarty Rookie
Joined: 27 Mar 2009 Posts: 25
|
Posted: Sun Feb 19, 2012 6:51 pm Post subject: |
|
|
@rodneyrehm you are 100% correct.
I've been emailing @U.Tews about this issue (and sent some sample code).. illustrating the problem.
I've spent a day looking through the most of the code (except for the lexer and parser they scare me ). And came up with a thought similar to yours. What I am thinking would be nice is a new smarty class member variable, something like:
public $assignmentScope = Smarty::SCOPE_LOCAL;
Which would be used by the assign() method, and the various compiler tags, and anywhere else there is an 'assign' attribute to a tag, to decide whether the assignment should 'bubble up' or stay at it's current level. For smarty2 compatibility, the $assignmentScope variable could be changed to Smarty::SCOPE_ROOT ... a scope argument to the tag or to the assign() method could override this. |
|
Back to top |
|
Aristophan Smarty Regular
Joined: 10 Jan 2011 Posts: 96
|
Posted: Tue Sep 17, 2013 9:21 am Post subject: |
|
|
May I ask, if that ever came into practice? |
|
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
|