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

Adding Additional Values/Params to $visitor in template

Discussion in 'Styling and Customization Questions' started by TVictory, Sep 4, 2012.

  1. TVictory

    TVictory New Member

    How could i add values to the $visitor variable and get them out of sidebar_visitor_panel among other places?

    Integration to our main site requires that we show some information about the user that is not in the xf database, or on the forum. Example: Number of comments on a custom made blog.

    One of the places this value will need to be shown is in the template sidebar_visitor_panel

    I have tried adding data using the listener container_public_paramas

    Something to the affect of:

    class CBlog_Stats {
      static function fbb_user(array &$params, XenForo_Dependencies_Abstract $dependencies){
       $params['visitor']['blog_stats'] = Array(

    but this hasn't worked.
  2. Chris D

    Chris D XenForo Developer Staff Member

    You could probably do this with a template hook listener.

    There's a hook you can use called "sidebar_visitor_panel_stats" which will allow you to append data to the stats section, where the message count etc. is.

    So it would look something like this:

    class YourAddOn_Listener
    public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)​
    if ($hookName == 'sidebar_visitor_panel_stats')​
    $hookParams['visitor']['blog_stats'] = array(​
    $contents .= $template->create('your_addon_template', $hookParams);​

    That would then able you to use {$visitor.blog_stats.num_posts} and {$visitor.blog_stats.num_upvotes} in your template.
    Jake Bunce and TVictory like this.
  3. TVictory

    TVictory New Member

    Thats definitely reasonable for this case and will probably get me pretty far, but if there is away to add values to $visitor directly i would still like to know how.

    Thanks for your help.
  4. Chris D

    Chris D XenForo Developer Staff Member

    If there is, I don't know about it (quite plausible) but then again I've never needed it.

    Generally, as $visitor is essentially a template parameter you'd want to do most template things using a template hook but there may be other ways to achieve that. I'm sure someone like Jake will have some more suggestions.
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The question is why do you want to add values to $visitor? The only reason would be to make those values available everywhere $visitor is used. But for just one sidebar addition it doesn't warrant changing the $visitor object. Just retrieve the value and use it in that one place.
    Chris D likes this.
  6. TVictory

    TVictory New Member

    Yes i may want it everywhere and i may want it for a few users, so i can sort/display users by added values.
  7. Chris D

    Chris D XenForo Developer Staff Member

    Everywhere is a hell of a lot of templates!

    It still makes sense to add it to each hook it is required.
  8. TVictory

    TVictory New Member

    right but seeing as there is container_public_paramas you can see how some things would help to be global. At the moment i am good, just i could think of reasons with external data where would be nice to be able to edit the visitor directly.

Share This Page