Lazy Loading Theme Variables

mfer's picture

When a page.tpl.php file is generated all of the variables are populated with content. This includes all the regions, the css and javascript, and everything. Some of these are never used in some themes (e.g., $mission) and some are often rebuilt later like $styles and $scripts.

I propose we lazy load the content in these variables when the variables are called the first time which will typically be in the template files. Since lazy loading is a feature of objects it will mean these variables will be attached to objects.

So, in drupal 6 you theme would have:

print $styles;

The proposed change would make these:

print $template_object->styles;

The name $template_object is just a working name.

The reason for this desired change is performance and scalability. For example, currently (in D5 and D6) all the regions are loaded for a page.tpl.php file. So, if you have a lot of regions they are loaded every time. If we move to lazy loading the variables that means the regions will be lazy loaded. Only when one is printed in a page.tpl.php file will it be loaded.

So, you could have 20 regions on your site. Stick 5 in your page.tpl.php file, put a different 5 in your page-front.tpl.php file, stick a couple in your node.tpl.php file (I use this to stick a related content block between my node and comments on a blog), and stick them where you please. Then, the regions content is only loaded if the variable is printed in a template file.

On top of that, any variables provided in drupal that you don't use won't cause a performance hit against your page. They will never be generated if you don't use them.

Thoughts? Suggestions?


Makes a lot sense...

DeeZone's picture

Really interesting concept. Would this actually make for improved performance considering that there would still have to be some processing to weed out what content was needed. I assume that database quires are more efficient that PHP? Perhaps rather that using the presence of a call to a region in page.tpl.php to be the trigger the user could define what regions go with which pages in the .info file.

No Added Database Queries

mfer's picture

Database queries are ofter slower than PHP. But, the current method and lazy loading both happen entirely in PHP. What we would be taking advantage of is the __get() magic method for an object. This would happen transparently, though.

There would be no need to define which regions go with which pages in the .info files.

Matt Farina

Theme development

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: