What's the correct way of calling a template from within a listener?

TheBigK

Well-known member
I'm creating a small add-on that lets the admin write notes for any user on their profile in a special tab. So far-

1. I've created UserNotes/Listener/NotesTab.php

PHP:
class UserNotes_Listener_NotesTab
{
    public static function template_hook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {

        // Following if check creates the heading of the tab
        if ($hookName == 'member_view_tabs_heading')
        {
            $contents .= '<li><a href="{$requestPaths.requestUri}#notes"> Notes </a></li>';
        }

        // Following if check creates the contents of the tab

        if ($hookName == 'member_view_tabs_content')
        {
            $contents .= '
            <ul id="ProfilePanes">
            <li id="info" class="profileContent">
            <div class="section">
            <h3 class="textHeading"> The Boss Left Following Notes For You </h3>
            <div class="primaryContent">

                 ||| THIS IS WHERE I NEED TO INCLUDE MY TEMPLATE |||

            </div>
            </div>
            </li>
            </ul>
            ';
        }
    }
}

My questions:-

1. Should I transfer the entire content, which $contents will have to the template that I wish to include?

2. Or is my current method of calling the template is correct? If yes, how do I call my template from inside the single quotes?

PS: I am aware that I need to preLoad template in order for them to work. I'm yet to do that in my code.
 
Update:

I got this to work by moving the html component of the $contents to the template I'm including and then using $template->create(). I appended the output of the template create (and then rendered) to my $contents array.

Need confirmation from someone that this is indeed the way to go about it.
 
If you want to make it very simple, you can also write the whole custom HTML into the template modification. Then you do not need a controller at all. Also no listener and even no template. ;-)
 
Thanks @HWS .

I'd really appreciate if you could tell me whether I'd be required to manually create the template modification? I mean, what'd be the recommended way to add a tab on the member profile page (without using hooks)?
 
Yes, manually create a template modification for the tab title and another one for the content.

Write your content into the modification and you are set. ;-)
 
Yea if you have the RM installed or Media Gallery they both add content on profiles with a tab using Template Modifications so you can see how it is done. Depending where you do your find and replace you may need to change the execution order for your template modification.
 
Template Hooks aren't DEPRECATED. Deprecated implies they will eventually go away.

There are still many reasons to use template hooks over the template modification system.
 
  • Like
Reactions: 1im
Okay, now this is interesting. Is there any official word on whether Template Hooks will be depreciated (I'm curious to know whether they'll continue to be in XF 2.0).
 
Template hooks are useful for some places where the TMS system doesn't work. Bit lazy to search atm but I believe Mike also said that there are times when the template hooks system will still be preferred, since the TMS system can't do it. Though the TMS system will usually be better for most case scenarios, such as this one. You can very easily make a Template Modification to add these in, and if you distribute your add-on then these are included in your .xml file.
 
Template hooks are useful for some places where the TMS system doesn't work. Bit lazy to search atm but I believe Mike also said that there are times when the template hooks system will still be preferred, since the TMS system can't do it. Though the TMS system will usually be better for most case scenarios, such as this one. You can very easily make a Template Modification to add these in, and if you distribute your add-on then these are included in your .xml file.
I've seen that the template modifications are still, in a way, using the hooks. For example, if I've to add a new tab at the end of existing tabs on member_view page - I will have to search and replace the existing template hook from the TMS.

If I decide on not using the hooks altogether - is there a way I can achieve the same thing [Adding a new tab at the end of existing tabs, and loading content in it?]
 
They're not using the hooks, they're just using the hook text to do the find.

You can use any part of the template code to do the find on - sometimes the hook text is the easiest/most convenient.
 
Yes, but that applies to any part of the template code which may be changed or removed in a future release.
 
Back
Top Bottom