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

Issues with using templates

Discussion in 'XenForo Development Discussions' started by Robust, Oct 20, 2014.

  1. Robust

    Robust Well-Known Member

    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 &$paramsXenForo_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 $hookParamsXenForo_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!
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  3. Robust

    Robust Well-Known Member

  4. Robust

    Robust Well-Known Member

    Issue solved, not sure what it was but it works now I guess. Going to look into the Template Modifications system ASAP.
     
    Chris D likes this.

Share This Page