|
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 |
frodeborli Smarty n00b
Joined: 18 Mar 2013 Posts: 2
|
Posted: Mon Mar 18, 2013 12:37 pm Post subject: Lazy Variables |
|
|
Some variables might not be used, but are still fetched from the database or precalculated. Performance wise it would make sence to NOT fetch/initialize those variables for every rendering of the template.
Smarty should support lazy variables, perheps implemented like this:
Code: | $smarty->assign('friendList', function(Smarty $smarty) {
return fetchFriends(userid());
}); |
The workaround I use now is:
Code: | $lazyUserVars = new LazyVars();
$user->getFriends = function() { return fetchFriends(userid()); }
$smarty->assign('uservars', $lazyUserVars); |
Which can then be accessed in Smarty as $uservars->getFriends
Code for LazyVars:
Code: | /**
* LazyVars from Fubber Framework
* @author Frode Børli
*/
class LazyVars {
protected $_callables = array();
public function __get($name) {
if(!isset($this->_callables[$name]))
return NULL;
return call_user_func($this->_callables[$name]);
}
public function __set($name, $value) {
if(is_callable($value))
$this->_callables[$name] = $value;
else
$this->$name = $value;
}
} |
|
|
Back to top |
|
frodeborli Smarty n00b
Joined: 18 Mar 2013 Posts: 2
|
Posted: Mon Mar 18, 2013 12:45 pm Post subject: |
|
|
To clarify;
Provided that the designer does not use the variable, that the developer made available for the website - this makes sence.
This can happen if for example the developer redesigns the website, and removes the list of friends from somewhere.
The alternative:
The much more labor intensitive way of doing this is by creating a plugin. The plugin could assign the friend list to a variable like this:
Code: | {fetchFriends assign="friends"} |
This requires much more code and documentation for the designers. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Mar 18, 2013 1:22 pm Post subject: |
|
|
Smarty 3.2 will have a default variable handler. |
|
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
|