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

Passing variables to template not working.

Discussion in 'XenForo Development Discussions' started by Renari, Dec 25, 2012.

  1. Renari

    Renari Member

    I have an array $user that I want to pass all of it's contents to a template.
    PHP:
    $user = array(username => 'test'avatar =>'http://xenforo.com/community/styles/default/xenforo/logo.png')
    $params $template->getParams();
    $hookParams += $user;
    $params += $hookParams;
    $content .= $template->create('template_name'$params);
    Then in the template:
    HTML:
    <li>
    <a href="http://xenforo.com"><img src="{$avatar}" alt="{$username}"></a>
    </li>
    And... the output:
    HTML:
    <li>
    <a href="http://xenforo.com"><img src="" alt=""></a>
    </li>
    Not sure what I'm doing wrong.
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The keys should be quoted, and you are missing a semicolon:

    Code:
     $user = array('username' => 'test', 'avatar' =>'http://xenforo.com/community/styles/default/xenforo/logo.png');
    $params = $template->getParams();
    $hookParams += $user;
    $params += $hookParams;
    $content .= $template->create('template_name', $params); 
    
    And if you aren't using the other params then you might as well just pass that array directly:

    Code:
    $content .= $template->create('template_name', array(
    	'username' => 'test',
    	'avatar' => 'http://xenforo.com/community/styles/default/xenforo/logo.png'
    ));
    
     
    bousaid likes this.
  3. Renari

    Renari Member

    Hm, that's not actually my problem here's the entire hook.
    PHP:
            if ($hookName == 'forum_list_sidebar')
            {
                
    $sidebar '';
                
    $db XenForo_Application::get('db');
                
    $query $db->query("
                    SELECT
                    xf_user.user_id, username, gravatar, avatar_date, stream_data, field_value
                    FROM
                    xf_user, userstreams, xf_user_field_value
                    WHERE
                    xf_user.user_id = userstreams.user_id
                    AND
                    xf_user.user_id = xf_user_field_value.user_id
                    AND
                    field_id = 'twitch'
                    AND
                    field_value != ''"
    );
                while (
    $user $query->fetch()) { 
                    
    $user['stream_data'] = json_decode($user['stream_data'], true);
                    
    $user['stream_data'] = $user['stream_data'][0];
                    
    $user['profile'] = Xenforo_Link::buildPublicLink("members"$user);
                    
    $user['stream'] = $user['field_value'];
                    if (
    $user['gravatar']) {
                        
    $user['avatar'] = XenForo_Template_Helper_Core::getAvatarUrl($user's');
                    }
                    elseif (!empty(
    $user['avatar_date'])) {
                        
    $user['avatar'] = XenForo_Template_Helper_Core::getAvatarUrl($user's''custom');
                    }
                    else {
                        
    $user['avatar'] = XenForo_Template_Helper_Core::getAvatarUrl($user's''default');
                    }
                    
    $params $template->getParams();
                    
    $hookParams += $user;
                    
    $params += $hookParams;
                    
    $sidebar .= $template->create('UserStreams_Stream'$params);
     
                }
                
    $params $template->getParams();
                
    $hookParams['content'] = $sidebar;
                
    $params += $hookParams;
     
                
    $contents .= $template->create('UserStreams_Sidebar'$params);
            }
        }
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    To troubleshoot I really need full access so I can see and debug the data, and also know what you are trying to accomplish.
     
  5. Renari

    Renari Member

    Sorry it's taken me so long to reply. Doing a var_dump() of the variables being sent to the template indeed shows that they are being passed. If you would like to take a look at it first hand however I can give you access to look at it.

    Basically there is a named variable avatar being passed to the template, however {$avatar} in the template is outputting nothing.

    Example of $params array:
    [​IMG]
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Everything looks correct.

    It's odd that your user fields are all individual params. Normally the entire $user record would be its own entry.

    Code:
    $hookParams += array('user' => $user);
    
    That way you can access {$user.avatar} in the template instead of {$avatar}.

    Sure, give me access and I will take a look.
     

Share This Page