MyBB Community Forums

Full Version: Faster templates and other bits and bobs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I saw your ad for a developer position and thought against it seeing as I'm not an active member of the community and I just don't have any time for it. But, I was once a board developer and I picked up a couple of things along the way, so I thought I'd give you a couple of little things to see whether you liked them or not.

So, MyBB, as with most PHP boards, parses its templates like this:

# Some example loop
foreach($loop as $whatever)
{
    eval("\$content = \"".$templates->get("index_loop")."\";");
}
eval("\$index = \"".$templates->get("index")."\";");

The eval() method has served the forum industry well for years and years, but a few years ago I decided there might be a faster way.

Each time you go through a loop that generates HTML from a template bit, you incur extra overhead as the PHP parser parses the code inside the eval() statement. If you were to delcare your template as a function inside your eval() you could dodge that overhead and have your page display a bit quicker.

Essentially what I am proposing is something like this:

class templates
{
    function cache($templates)
    {
        $to_eval = '';
        # Load templates from DB
        $q = $db->query(whatever);
        while( $row = $db->fetch_array($q) )
        {
            $to_eval .= 'function tpl_'.$row['title'].'($tplvars) { return "'.$row['template'].'"; }';
        }
        eval($to_eval);
    }
}

Then you could call a template function whenever you needed to generate some HTML and take calls to eval() out of loops.

I actually implemented this on the index and forum view pages of OpenBB 1.0.8 but being the lazy goon I am I didn't test it for speed Toungue

The only drawback is the mod-writers would probably need to rewrite any plugin that passed data through to the HTML to use the new $tplvars variable.

Oh, and the other thing I wanted to mention was, don't have a separate query to find today's birthdays on the index page, cache them in the settings table every 24 hours. You'll need two caches, one today's and one tomorrow's, to account for time zones.
Peter Hall Wrote:Oh, and the other thing I wanted to mention was, don't have a separate query to find today's birthdays on the index page, cache them in the settings table every 24 hours. You'll need two caches, one today's and one tomorrow's, to account for time zones.

I asked Chris about using a cache for those and his response was almost EXACTLY like yours. That's just creepy. Are you sure you two aren't related?
Oh my god... you may be onto something... I'm a kangaroo in disguise too!

All my life I've been living a lie... *sniff*