• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Issues with using templates

Robust

Well-known member
#1
I'm following a tutorial and have the following code. I also have two code event listeners, one listening for both of these methods (I think that's what they are haha)

PHP:
<?php
class newProfileTabs_Listener {

    public static function template_create ($templateName, array &$params, XenForo_Template_Abstract $template)
    {
        switch ($templateName) {
            case 'member_view':
                $template->preloadTemplate('newProfileTab_ourTab');
                $template->preloadTemplate('newProfileTab_ourTab_content');
                break;
        }
    }

    public static function template_hook ($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)    {
        switch ($hookName)
        {
            case 'member_view_tabs_heading':
            {
                $tabTemplate = $template->create('newProfileTabs_ourTab', $template->getParams());
                $rendered = $tabTemplate->render();
                $contents .= $rendered;
                break;
            }
            case 'member_view_tabs_content':
            {
                $tabTemplate = $template->create('newProfileTabs_ourTab_content', $template->getParams());
                $rendered = $tabTemplate->render();
                $contents .= $rendered;
                break;
            }
        }
    }
}
It worked fine when I had the templates in code, but I'm using the template system in XenForo and the tab no longer shows. Not sure why?

Also, is it necessary to pre-load templates? Why should/should you not do it?

Finally, still a little confused on what the & does when it's before a variable name (&$contents)

Thanks!
 

Chris D

XenForo developer
Staff member
#2
Which tutorial are you following?

There's no particular reason why your code shouldn't work as long as you've created the listeners and templates properly.

However, the method of using template hooks is deprecated and no longer recommended. (It works, but there's no guarantee they'll still be included in, say, XF 2.0). Since XenForo 1.2 there is the Template Modifications system and this is recommended for add-on developers over using template hooks.