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

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

Discussion in 'XenForo Development Discussions' started by TheBigK, Dec 16, 2014.

  1. TheBigK

    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

    class UserNotes_Listener_NotesTab
        public static function 
    template_hook($hookName, &$contents, array $hookParamsXenForo_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 |||

    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.
  2. TheBigK

    TheBigK Well-Known Member


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

    HWS Well-Known Member

    Just a short notice: Template Hooks are deprecated. You should use template modifications for your add-on.

    Your individual template is called from ControllerPublic. This file has to be created and called by a listener.
    Thomas.B, Robust, SneakyDave and 2 others like this.
  4. HWS

    HWS Well-Known Member

    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. ;-)
    Steve F likes this.
  5. TheBigK

    TheBigK Well-Known Member

    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)?
  6. HWS

    HWS Well-Known Member

    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. ;-)
    TheBigK likes this.
  7. Steve F

    Steve F Well-Known Member

    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.
    LPH, SneakyDave and TheBigK like this.
  8. TheBigK

    TheBigK Well-Known Member

    Thanks, Gotta explore both the addons.
  9. Jaxel

    Jaxel Well-Known Member

    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.
    1im likes this.
  10. TheBigK

    TheBigK Well-Known Member

    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).
  11. Brogan

    Brogan XenForo Moderator Staff Member

    They are indeed deprecated.
    TheBigK likes this.
  12. Robust

    Robust Well-Known Member

    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.
  13. TheBigK

    TheBigK Well-Known Member

    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?]
  14. Brogan

    Brogan XenForo Moderator Staff Member

    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.
  15. TheBigK

    TheBigK Well-Known Member

    But in future, if the hooks are totally removed from XF, won't that break the addon?
  16. Brogan

    Brogan XenForo Moderator Staff Member

    Yes, but that applies to any part of the template code which may be changed or removed in a future release.

Share This Page