View previous topic :: View next topic |
Author |
Message |
mankyd Smarty Regular
Joined: 04 May 2006 Posts: 92 Location: Boston MA
|
Posted: Sun Jan 27, 2008 8:26 pm Post subject: Variable Namespaces |
|
|
I have run into a situation for which some sort of namespace functionality would be incredibly useful and perhaps even provide a level of security/safety.
Two completely separate parts of my code are both using Smarty. I say that they are completely separate because they are not explicitly aware of each others internal workings. They may, however, have execution paths that intermingle with each other or occur in an otherwise undefined manner. It is therefore conceivable that function-A sets a Smarty variable, expecting it to remain set until it is ready to display a template. In the mean time, function-B comes along and changes that value in a way the function-A could not predict, causing problems.
Even more likely, function-A's template might behave differently depending on whether a particular variable is set or left undefined. If function-B comes along and sets that variable, it might cause function-A's template to behave incorrectly.
It would be great if Smarty implemented a name-space like feature to resolve this. For instance, you might assign the variable as such:
Code: | $smarty->assign('my_namespace:variable_name', 'value'); |
and then display the template with something similar to:
Code: | $smarty->display('foobar.tpl', 'cache_id', 'compile_id', 'my_namespace'); |
or you could simply have a single function that moves all Smarty functions from one namespace to another:
Code: | $smarty->use_namepspace('my_namespace');
$smarty->assign('var', 'val');
$smarty->display('foobar.tpl'); |
Behavior similar to this can sort of be mimicked using associative arrays but it is hardly a good substitute. |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Mon Jan 28, 2008 12:59 am Post subject: |
|
|
You could just instantiate Smarty twice.
Or you "assign" the template variables to an associative array (at the end of the day, assign() does exactly that) and switch like this
Code: | $smarty->clear_all_assign();
$smarty->assign($my_namespace);
$smarty->display('foobar.tpl'); |
This might not be the most efficient way though, since assign() will loop through your array to assign each key/value pair. _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Jan 28, 2008 2:02 am Post subject: |
|
|
You can set a separate compile_id and cache_id for each system, or give them separate cache_dir and compile_dir settings. |
|
Back to top |
|
mankyd Smarty Regular
Joined: 04 May 2006 Posts: 92 Location: Boston MA
|
Posted: Mon Jan 28, 2008 2:34 am Post subject: |
|
|
celeb: Your second suggestion is close, though it doesn't solve the problem of assigning variables that, otherwise unset, affect the output.
I should elaborate on one point. The system I am making is plugin framework. The reason namespaces would be helpful is because it is currently impossible to guarantee that one plugin does not step on another plugin's toes by way of the templating engine.
As for instantiating Smarty multiple times, this is an intriguing possibility, though it does add overhead. The way I have my system architected currently, it's possible that each plugin would want to have it's own namespace and thus it's own Smarty instance; there may be several plugins running at any one time (10 or more.) Would not this be wasteful of resources?
mohrt: changing the compile_id does not change what variables are visible to a call to display does it? i.e.
Code: | $smarty->assign('var', 'val');
$smarty->display('foo.tpl', 'foo', 'foo');
//several function calls later...
$smarty->display('bar.tpl', 'bar', 'bar'); //still sees $var, correct? |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Jan 28, 2008 2:51 am Post subject: |
|
|
I see what you are saying, you have different calls during one invocation that you want separate variable namespaces. The easiest would be separate smarty instances.
All template vars are held in $smarty->_tpl_vars, so you could extend the class and add your own namespace requirements that shuffle the assigned variables. Or just clear the assigned variables if you are not going from A to B and back to A. |
|
Back to top |
|
genroxbro Smarty Rookie
Joined: 29 Jul 2009 Posts: 6 Location: Canada
|
Posted: Sun Aug 02, 2009 10:16 am Post subject: Variable Namespaces |
|
|
if you didnt need to see the value of a variable all the time you could use a Display Variable step and trigger it when you wanted to see the value or there is the Variables window that you use to setup the variables. It floats so that you can see your variables when debugging but you could use it for this also.
_____________________________________
Website I designed for online payday loans in canada company. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Aug 02, 2009 7:07 pm Post subject: |
|
|
You could create your own namespace:
Code: | $vars['ns1']['foo'] = 'bar1';
$vars['ns2']['foo'] = 'bar2';
$smarty->assign($vars); |
template:
Code: | {$ns1.foo}
{$ns2.foo} |
you could also use your own object for fetching vars and have very fine grained control over them:
Code: | $myobj->setNamespace('ns1');
$myobj->getVar('foo'); |
In Smarty 3, you can use a PHP stream to do whatever you want:
Code: | {$ns1:foo} {* returns from PHP: file_get_contents('ns1://foo'); *} |
|
|
Back to top |
|
|