Converting poorly-coded template hook into one with a custom template and precaching

Status
Not open for further replies.

greger1911

OMG Member
Nov 29, 2011
1
0
35
42
I've got this template hook I use to add an extra sidebar element. Can someone guide me through the process of converting this into a template and precaching it in order to reduce database and system load? I understand that spitting the HTML out like I'm doing right now is undesired.

Thank you.

PHP:
<?php
class greg_ShowThing_Plugin_Plugin
{
    public static function showThing($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if($hookName == 'forum_list_sidebar')
        {
            $db = XenForo_Application::get('db');
            $query_results = $db->fetchRow("SELECT * FROM `cooltable` WHERE id=1");
           
            $thing = $query_results['thing'];
            $thing2 = $query_results['thing2'];
           
            $contents .= '
            <div class="section">
                <div class="secondaryContent statsList">
                    <h3>Thing</h3>
                    <div class="pairsJustified">
                    <dl><dt>Thing:</dt>
                        <dd>'.$thing.'</dd></dl>
                    <dl><dt>Thing 2:</dt>
                        <dd>'.$thing2.'</dd></dl>
                    </div>
                </div>
            </div>
            ';
           
        }
    }
}
 

Floris

I'm just me :) Hi.
Staff member
Jan 1, 2001
60,101
1,425
930
47
Netherlands
mrfloris.com
This is what I use to pre-cache a template that I use in a plugin.
PHP:
public static function templateCreate(&$templateName, array &$params, XenForo_Template_Abstract $template)
{
switch ($templateName)
{
case 'forum_list':
{
$template->preloadTemplate('xenfans_promoted');
break;
}
}
}

case line has the HOOK

Don't forget to create a code event listener for template_create

Then create a new template for your plugin matching that name of course (in example: xenfans_promoted)
Then you can make a template_hook code event listener, with php code to match

PHP:
public static function templateHook($name, &$contents, array $params, XenForo_Template_Abstract $template)
{
switch ($name)
{
case 'forum_list_sidebar':
{
$temp_contents = $template->create('xenfans_promoted', $template->getParams());
$contents = $temp_contents . $contents;
break;
}
}
}

In the above code you can see me get my template, and put it in $temp_contents, which I then prepend to $contents;

I notice you do a query, and populate some strings.

What you could do is put that in array for $viewParams, and include it in your return response viewer.

PHP:
       $viewParams = array(
'thing' => $thing,
'thing2' => $thing2
);
return $this->responseView('', 'xenfans_whatsnew_page', $viewParams);

I hope this gets your a bit of extra information to get you started, it might not be a 100% what you're looking for, but do let me know if you made progress or still have issues.
 
Status
Not open for further replies.