Passing custom variables into a template modification?

Jarvis

Member
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.
 
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.
 
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:

15pqcoY.png


I'm trying to send variables to the
Code:
Addon_navLinker
template. Do I just use a normal Controller Public and Route?
 
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 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>
 
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)
 
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?
 
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
 
Currently I have:

15pqcoY.png


I'm trying to send variables to the
Code:
Addon_navLinker
template. Do I just use a normal Controller Public and Route?
Thats a bad replace... it basically deletes the hook and everything within it.
 
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.
 
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
 
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>
 
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.
 
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 Bottom