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

Variable Namespaces

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
mankyd
Smarty Regular


Joined: 04 May 2006
Posts: 92
Location: Boston MA

PostPosted: Sun Jan 27, 2008 8:26 pm    Post subject: Variable Namespaces Reply with quote

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
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Jan 28, 2008 12:59 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Jan 28, 2008 2:02 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mankyd
Smarty Regular


Joined: 04 May 2006
Posts: 92
Location: Boston MA

PostPosted: Mon Jan 28, 2008 2:34 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Jan 28, 2008 2:51 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
genroxbro
Smarty Rookie


Joined: 29 Jul 2009
Posts: 6
Location: Canada

PostPosted: Sun Aug 02, 2009 10:16 am    Post subject: Variable Namespaces Reply with quote

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
View user's profile Send private message Send e-mail
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Aug 02, 2009 7:07 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
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 -> Feature Requests 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