MyBB Community Forums

Full Version: How does MyBB's template system work?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
A note to those running plugins. The tutorial for the plugins only shows how to load $templatelist from a new page and not how to do it using the plugin file. To do that you need to hook into global_start and do a global to $templatelist and add your templates that way. This is important if you want to avoid a query for each custom template added by the plugin.

I know a lot of authors don't optimize their plugins to utilize the cache.
(2017-02-14, 10:40 PM)labrocca Wrote: [ -> ]A note to those running plugins.  The tutorial for the plugins only shows how to load $templatelist from a new page and not how to do it using the plugin file.  To do that you need to hook into global_start and do a global to $templatelist and add your templates that way.  This is important if you want to avoid a query for each custom template added by the plugin.

I know a lot of authors don't optimize their plugins to utilize the cache.

I've not looked to deeply into the the templating system but would that not then create additional load on the pages tha do not require the templates used in the plugin?

When the cache method is called in global.php it explodes the $templatelist variable into an array and then builds the query with the templates titles and and then fetches the data from the title and pushes it to the $cache property. 

Adding unused templates to global_start would just mean that templates become unnecessarily loaded on every page load.
You are correct, but it is the more efficient way of doing it given the way templates are stored. I would say the slight increase in memory usage to load in another template would be preferable to additional queries for a single template (especially if you have 15 plugin templates being loaded, would be 15 queries).

There are only really two options - cache them all, even though they may not be used, or only load them strictly when they're required, which would mean individual queries per-template. Because they're pulled from a database per-request and the cache doesn't persist in the filesystem or anything, there's limitation on how to load them.

On this forum, the query to load all templates returned 1143 rows and took 6ms - adding in another, say, 15 plugin templates here would not have any measurable effect on this query, whereas 15 new queries would.

Yes, instead of adding all templates for a plugin, you could maybe only add them if they might be used on that page (i.e. if a template was used in the postbit, only add it to $templatelist if THIS_SCRIPT == 'showthread.php'), however if the template calls are buried deep in logic, you may still cache a template that doesn't get used on that request, and like I say, caching them all would add negligible overhead anyway. In the same way, the core files will cache every template used in that file, but there's no guarantee every one of them is going to be used, so really, plugins doing the same is the best way to go about it.
(2017-02-15, 01:07 PM)Matt Wrote: [ -> ]You are correct, but it is the more efficient way of doing it given the way templates are stored. I would say the slight increase in memory usage to load in another template would be preferable to additional queries for a single template (especially if you have 15 plugin templates being loaded, would be 15 queries).

There are only really two options - cache them all, even though they may not be used, or only load them strictly when they're required, which would mean individual queries per-template. Because they're pulled from a database per-request and the cache doesn't persist in the filesystem or anything, there's limitation on how to load them.

On this forum, the query to load all templates returned 1143 rows and took 6ms - adding in another, say, 15 plugin templates here would not have any measurable effect on this query, whereas 15 new queries would.

Yes, instead of adding all templates for a plugin, you could maybe only add them if they might be used on that page (i.e. if a template was used in the postbit, only add it to $templatelist if THIS_SCRIPT == 'showthread.php'), however if the template calls are buried deep in logic, you may still cache a template that doesn't get used on that request, and like I say, caching them all would add negligible overhead anyway. In the same way, the core files will cache every template used in that file, but there's no guarantee every one of them is going to be used, so really, plugins doing the same is the best way to go about it.

So overall there isn't a "good" way of handling it but the better way would be adding them to global to minimise the amount of queries run.

Thanks for that.
If there was a hook in the $templates class one could actually create a file cache system of some kind.
Pages: 1 2