Values in template array

dbembibre

Active member
I trying to validate in a listener class if the user is array in this case i inject a special template.
When i get $template->getParams() i can see via print_r all values, but no cant acces to it.
Whats the problem ??

I see in print_r($params)
[is_admin] => 1
[username] => Danny
But when i asign to a vars they dont have any value.

PHP:
<?php
class microstats_Listener
{
    public static function template_hook ($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'footer_links_legal')
        {
       
            $params = $template->getParams();
            print_r($params);
            $isAdmin = $params ['is_admin'];
            $AdminName = $params['username'];
Anyone can help me please.
Thanks in advance
 

Chris D

XenForo developer
Staff member
Template hooks are designed to inject new content into existing templates. You normally modify the parameters that already exist in the $template object and pass them through to a new template.

For example,

PHP:
<?php
class microstats_Listener
{
    public static function template_hook ($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'footer_links_legal')
        {
       
            $params = $template->getParams();
            print_r($params);
            $isAdmin = $params ['is_admin'];
            $AdminName = $params['username'];

            $contents .= $template->create('your_new_template', $params);
        }
     }
}
That would add your modified $params into a template called 'your_new_template' and it will append it to the end of the existing $contents of the footer_links_legal hook.

You might also want to play around with:

PHP:
$template->setParams($params);
That may overwrite the existing template params with your modified params if you didn't need to append an additional template.
 

dbembibre

Active member
Thats is that i need. Thanks a lot :D


Template hooks are designed to inject new content into existing templates. You normally modify the parameters that already exist in the $template object and pass them through to a new template.

For example,

PHP:
<?php
class microstats_Listener
{
    public static function template_hook ($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'footer_links_legal')
        {
      
            $params = $template->getParams();
            print_r($params);
            $isAdmin = $params ['is_admin'];
            $AdminName = $params['username'];

            $contents .= $template->create('your_new_template', $params);
        }
     }
}
That would add your modified $params into a template called 'your_new_template' and it will append it to the end of the existing $contents of the footer_links_legal hook.

You might also want to play around with:

PHP:
$template->setParams($params);
That may overwrite the existing template params with your modified params if you didn't need to append an additional template.
 

dbembibre

Active member
Cant get to work, my last vbulletin plugin was years ago :D maybe i convert in a retarded :D
I make a litle add-on to show the server load to admin only in the footer, all work ok, i adquire the load, render the template but cant see the variables passed to template, only can see if i do a
<xen:helper dump, $varname>

My code: the listener.
PHP:
<?php
class microstats_Listener
{
    public static function template_hook ($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'footer_links_legal')
        {   
            if ($loadavg = @file_get_contents("/proc/loadavg"))
            {
                $regs = split(" ",$loadavg);
                $serverload = ' [Server Loads: <b>' . $regs[0] .'</b> ' . $regs[1] . ' : ' . $regs[2] . ']';
            }elseif ($stats=@exec('uptime')) {
   
                preg_match('/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/',$stats,$regs);
                $serverload = ' [Server Loads: <b>' . $regs[1] .'</b> ' . $regs[2] . ' : ' . $regs[3] . ']';
   
            }elseif ($loadavg = @`sysctl vm.loadavg|cut -d" " -f3-5`) {
                $regs = explode(" ",$loadavg);
                $serverload = ' [Server Loads: <b>' . $regs[0] .'</b> ' . $regs[1] . ' : ' . $regs[2] . ']';
   
            }
           
            $serverload2 .='<!-- micro server stats -->' . $serverload;
           
            $dArray = array(
                        "serverload" => $serverload2,
                        "testvar" => "hello",
                        );
                       
            $merged=array_merge($dArray, $template->getParams());
           
            $nTemplate = $template->create('microstats_label', $merged);
            $contents .= $nTemplate->render(); 
           
        }
    }
}
?>
My template: microstats_label
Code:
<xen:if is="{$visitor.is_admin}">
$serverload
</xen:if>

{xen:helper dump, $serverload}
{xen:helper dump, $testvar}
The output that i see:
HTML:
Terms and Rules $serverload
string(55) "<!-- micro server stats --> [Server Loads: <b>0.80</b> 0.64 : 0.67 ]"
string(5) "hello"
Thanks in advance

Template hooks are designed to inject new content into existing templates. You normally modify the parameters that already exist in the $template object and pass them through to a new template.

For example,

PHP:
<?php
class microstats_Listener
{
    public static function template_hook ($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    {
        if ($hookName == 'footer_links_legal')
        {
      
            $params = $template->getParams();
            print_r($params);
            $isAdmin = $params ['is_admin'];
            $AdminName = $params['username'];

            $contents .= $template->create('your_new_template', $params);
        }
     }
}
That would add your modified $params into a template called 'your_new_template' and it will append it to the end of the existing $contents of the footer_links_legal hook.

You might also want to play around with:

PHP:
$template->setParams($params);
That may overwrite the existing template params with your modified params if you didn't need to append an additional template.
 

dbembibre

Active member
Missing curly braces around that variable, so it should be {$serverload}. You might try that and see if it works.
Holly sheet, i waste one hour testing and testing the code, yes i'm retarded, thanks for the help
 
Last edited:
Top