Passing custom variables into a template modification?

#1
Hi there,

I've been reading up on template modifications, and trying to use them instead of template hooks from here on in.

However, I'm particularly trying to add custom content to the navigation_visitor_tab template. This content requires unique variables. I know how to create a template modification in the back end control panel, however I do not know how I can include custom variables in the viewParams of the loaded template when it's not my Addons ControllerPublic.

I've heard about extending the XenForo Controller, however I don't know where to begin with finding the correct controller or extending it in the correct manner.

Any help?

Cheers.
 

AndyB

Well-known member
#2
However, I'm particularly trying to add custom content to the navigation_visitor_tab template. This content requires unique variables. I know how to create a template modification in the back end control panel, however I do not know how I can include custom variables in the viewParams of the loaded template when it's not my Addons ControllerPublic.
Here's one idea, hope it helps. In the Template Modification you can make a standard link, then whatever you link to can have the variables you need.
 
#3
Here's one idea, hope it helps. In the Template Modification you can make a standard link, then whatever you link to can have the variables you need.
Currently I have:



I'm trying to send variables to the
Code:
Addon_navLinker
template. Do I just use a normal Controller Public and Route?
 

AndyB

Well-known member
#4
I suggest you first explain exactly what you are wanting to do. For example "I want a link here" and "the link does this".
 
#5
I suggest you first explain exactly what you are wanting to do. For example "I want a link here" and "the link does this".
I want to be able to add a custom link to the nav, but that $url is generated via php. How can I get the PHP variable to the template I 'include' in the template modification?

Code:
<a href="{$url}">My Link</a>
 

Jake B.

Well-known member
#6
Any variable in the template you're applying the modification to will be available in your template modification (and the template you include in it)
 

Jake B.

Well-known member
#7
I want to be able to add a custom link to the nav, but that $url is generated via php. How can I get the PHP variable to the template I 'include' in the template modification?
Why is the link generated in PHP? Can't you use the xen:link helper?
 
#8
Any variable in the template you're applying the modification to will be available in your template modification (and the template you include in it)
Yes, but how do I get variables into that template? I already understand the method of ControllerPublic -> Template (when called via route). How do I connect 'PHP' to the template when it isn't a route?

Why is the link generated in PHP? Can't you use the xen:link helper?
Sadly not, the variable needed is within a database
 

pegasus

Well-known member
#11
I had a similar problem yesterday. Ending up doing:
Code:
<xen:set var="$myVar"><xen:callback class="MyClass_Template_Callback" method="getMyVar" params="{xen:array 'varNeeded=myVar'}" /></xen:set>
<xen:include template="myTemplate" />
Unfortunately xen:callback can only return strings, so if you ever need an array back, I have no idea.
 
#12
Is it not something you can define as an option?

If so, you can then access it using $xenOptions.optionName.
Sadly no, it's a database value specific to a given user.

Essentially, I want to be able to compute the value I need in a controller, and send this value into a template to add a link to the navigation_visitor_tab template
 
#13
I had a similar problem yesterday. Ending up doing:
Code:
<xen:set var="$myVar"><xen:callback class="MyClass_Template_Callback" method="getMyVar" params="{xen:array 'varNeeded=myVar'}" /></xen:set>
<xen:include template="myTemplate" />
Unfortunately xen:callback can only return strings, so if you ever need an array back, I have no idea.
Interesting. So this sets the template $myVar to whatever the method 'getMyVar' returns inside of the MyClass_Template_Callback?

Surely I could Just use $myVar in the line following the <xen:set>?

e.g.
Code:
<xen:set var="$myVar"><xen:callback class="MyClass_Template_Callback" method="getMyVar" params="{xen:array 'varNeeded=myVar'}" /></xen:set>
<p>{xen:raw $myVar}</p>
 

Chris D

XenForo developer
Staff member
#14
If you're going down the template callback route, you can actually just have the callback return the rendered template.

The full signature and usage example for a template callback function is:
PHP:
public static function getMyVar($content, $params, XenForo_Template_Abstract $template)
{
    $myVar = null; // Get whatever your var is here however you need to.
    
    $viewParams = array(
        'myVar' => $myVar
    );
    return $template->create('Addon_navLinker', $viewParams);
}
That should do what you want without having to extend the existing controller.
 
#15
If you're going down the template callback route, you can actually just have the callback return the rendered template.

The full signature and usage example for a template callback function is:
PHP:
public static function getMyVar($content, $params, XenForo_Template_Abstract $template)
{
    $myVar = null; // Get whatever your var is here however you need to.
   
    $viewParams = array(
        'myVar' => $myVar
    );
    return $template->create('Addon_navLinker', $viewParams);
}
That should do what you want without having to extend the existing controller.

How i can do this in xF2
 
Top