Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Lazy Variables

 
Post new topic   Reply to topic    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
frodeborli
Smarty n00b


Joined: 18 Mar 2013
Posts: 2

PostPosted: Mon Mar 18, 2013 12:37 pm    Post subject: Lazy Variables Reply with quote

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
View user's profile Send private message
frodeborli
Smarty n00b


Joined: 18 Mar 2013
Posts: 2

PostPosted: Mon Mar 18, 2013 12:45 pm    Post subject: Reply with quote

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


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

PostPosted: Mon Mar 18, 2013 1:22 pm    Post subject: Reply with quote

Smarty 3.2 will have a default variable handler.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    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