container_public_params uncooperative

Jake B.

Well-known member
I'm working on an add-on that will require a variable to be available in all templates, so I tried to use the container_public_params code event with the following:

Code:
public static function containerPublicParams(array &$params, XenForo_Dependencies_Abstract $dependencies)
{
     $visitor = XenForo_Visitor::getInstance();
     $params['testParam'] = $visitor->hasPermission('test', 'testPermission');
}

It is running, and I am able to access the variable inside XenForo_Dependencies_Public::getEffectiveContainerParams however, I cannot use $testParam inside a template.
 
Container params are only valid in container templates.

You'll probably find that {$testParam} works fine in PAGE_CONTAINER, for example.

What you want to do can probably be achieved by listening to template_create.

XenForo Resource Manager and Media Gallery both do a similar tactic to make some permissions available to any templates, e.g.

PHP:
public static function templateCreate($templateName, array &$params, XenForo_Template_Abstract $template)
{
    if (self::$_hasTemplatePerm === null)
    {
        self::$_hasTemplatePerm = XenForo_Visitor::getInstance()->hasPermission('resource', 'view');
    }

    if (!isset($params['canViewResources']))
    {
        $params['canViewResources'] = self::$_hasTemplatePerm;
    }
}
 
Container params are only valid in container templates.

You'll probably find that {$testParam} works fine in PAGE_CONTAINER, for example.

What you want to do can probably be achieved by listening to template_create.

XenForo Resource Manager and Media Gallery both do a similar tactic to make some permissions available to any templates, e.g.

PHP:
public static function templateCreate($templateName, array &$params, XenForo_Template_Abstract $template)
{
    if (self::$_hasTemplatePerm === null)
    {
        self::$_hasTemplatePerm = XenForo_Visitor::getInstance()->hasPermission('resource', 'view');
    }

    if (!isset($params['canViewResources']))
    {
        $params['canViewResources'] = self::$_hasTemplatePerm;
    }
}

That worked perfectly. You've saved me once again, thanks!
 
Back
Top Bottom