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

Add Params to existing template with template hook without adding a template?

Discussion in 'XenForo Development Discussions' started by Marcus, Oct 8, 2012.

  1. Marcus

    Marcus Well-Known Member

    Can I add params to an existing template with template hook, without adding a template? This is not working, the variable test_variable is not given to the template navigation_visitor_tab.

    Code:
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template) {
    switch($hookName) {
    case 'navigation_visitor_tab': {
    $myParams = array('test_variable' => 1,);
    $mergeparams = array_merge($myParams, $template->getParams());
    $template->setParams($mergeparams);
    break;
    } }
    This works, but only in the added template my_template:
    Code:
     case 'body':{
    $myParams = array(test_var =>  1);
    $contents = $template->create('my_template', array_merge($myParams, $template->getParams())) . $contents;
    break;
    }
    Is it possible to just put a parameter into an existing template like navigation_visitor_tab?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Yes, it should be possible.

    The problem with your code, however, is that navigation_visitor_tab is the name of the template, rather than the name of a template hook.

    If you look in that template, you will notice the following hooks:

    navigation_visitor_tabs_start
    navigation_visitor_tab_links1
    navigation_visitor_tab_links2
    navigation_tabs_account
    navigation_visitor_tabs_middle
    navigation_visitor_tabs_end

    You will need to ascertain which one of these you want the parameter to be available to.

    This is how I normally do it.

    PHP:
        public static function templateHook($hookName, &$contents, array $hookParamsXenForo_Template_Abstract $template)
        {
            switch (
    $hookName)
            {
                case 
    'navigation_visitor_tabs_start':
     
                    
    $params $template->getParams();
                    
    $params['test_var'] = 1;
                    
    $params += $hookParams;
                    
    $contents .= $template->create('profile_view_count'$params);
                break;
            }
        }
     
    Jake Bunce and Marcus like this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    Spot the obvious mistake...

    Of course in your case, instead of $contents .= $template->create

    You'd be looking to do this:

    PHP:
    $template->setParams($params);
    But the rest of my code in the previous post is relevant.
     
    Jake Bunce likes this.
  4. Marcus

    Marcus Well-Known Member

    Thanks! The template profile_view_count would be a new template, right? Could I just add parameter to an existing template like navigation_visitor_tab without adding a new template in the hook?

    The idea is, I just want to show the "conversation" link so some user ids. I have created a file in ACP to put in all the user ids, and I also fetch the user ids in the Listener. I just want to give the $show_conversation variable in the navigation_visitor_tab template so I can put a if{...} around the conversation html.
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    I basically got carried away with my example... You may not need the $contents .= $template->create('profile_view_count', $params); line at all. Instead replace it with: $template->setParams($params);

    Overall, the only problem with your original code was probably that you were specifying a template name rather than a hook name so if you change that your code will probably work :)
     
    Jake Bunce likes this.
  6. Marcus

    Marcus Well-Known Member

    I put {$show_conversation} in template navigation_visitor_tab (after the hook of course).
    And I added this in my listener which works for other stuff:
    Code:
      case 'navigation_visitor_tabs_start': {
    
    $myParams = array('show_conversation' => 1);
    $mergeparams = array_merge($myParams, $template->getParams());
    $template->setParams($mergeparams);
    break;
    }
    Do you find any bug there? The {$show_conversation} variable should output a "1" instead nothing is displayed. Thank you so much!
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    I never use array_merge - I imagine it should work but to verify, you could put this before the break:

    Zend_Debug::dump($mergeparams);

    That should dump all of the params at the top of the page. If you paste its output here, that will tell us whether the array_merge worked properly.
     
    Jake Bunce likes this.
  8. Marcus

    Marcus Well-Known Member

    I used that line in the Listener.php before the break; but nothing is displayed. Maybe it doesn't work on this particular location. (it should be displayed on top of every page as navigation template is used everywhere.
     
  9. Chris D

    Chris D XenForo Developer Staff Member

    Have you created a code event listener in the admin cp yet?
     
  10. Marcus

    Marcus Well-Known Member

    Yes it all works out for the other template hooks. I just have entered a new case: ... for the navigation hook.
     
  11. Chris D

    Chris D XenForo Developer Staff Member

    Ahh... yes it would work for the other hooks...

    That particular hook starts and ends on that one line and actually you're passing the variable to the hook, not to the template. You would only be able to use the navigation_visitor_tabs_start in conjunction with an additional template or by adding stuff into its $contents

    I think passing content to the hook, probably via a new template would be best.

    I didn't quite understand what you were trying to achieve, but if you give me a specific example I'll try and work it out with you.
     
    Marcus likes this.
  12. Jeremy P

    Jeremy P Well-Known Member

    What I would consider a better plan for adding params to templates is to extend the controller(s) which use that template with something like:

    PHP:
    <?php
     
    class MarcusCoolAddon_ControllerPublic_SomeController extends XFCP_MarcusCoolAddon_ControllerPublic_SomeController
    {
     
    actionWhatever()
    {
    $response parent::actionWhatever();
     
    $response->params += array(
        
    'yourKey' => 'yourValue'
    );
     
    return 
    $response;
    }
    ..of course using the load_class_controller listener to extend the controllers. If you like I can provide a more concrete example.
     
  13. Marcus

    Marcus Well-Known Member

    yes that sounds more like MVC. I did a workaround by using this code
    Code:
    public static function templateCreate($templateName, array &$params, XenForo_Template_Abstract $template) {
     
    switch($templateName) {
     
      case 'PAGE_CONTAINER': {
    $myParams = array('show_conversation' => 1);
    $mergeparams = array_merge($myParams, $template->getParams());
    $template->setParams($mergeparams);
    break;
    I am just learning MVC so I am still struggling with it for now :)
     

Share This Page