Home    Articles


How to create XenForo custom dynamic template with callback


When a new custom page or dynamic page must be added in xenforo, basicly you need to create some php code plus we need to add the page in xenforo admin area, then set the template file that will be connected with the code.

1) Set up the code

In the picture above you can see that you must create a new folder in the library folder , in this case the folder is "Dev". You need another child folder and some php file.

In the php file add the code bellow:

 * Custom code 
 * This code will create a custom template with custom dinamic code
class Dev_Affportal_Tools
	public static function affportal(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract $response)
		$userModel = $controller->getModelFromCache('XenForo_Model_User');
		$visitor = XenForo_Visitor::getInstance()->toArray();
		#echo '<pre/>';print_r($visitor);exit;
		$response->params['user_id'] = $visitor['user_id'];
		$response->params['user_email'] = $visitor['email'];
		$response->templateName = 'custom_template';

2) Set the template

In admin area, create a new template file. ( Appereance->Templates->Create new template (button) )

Call it: custom_template

This file will contain the html code with code loops,ifs and for statements.

Here is an example of the template:

<h1>You are logged as:</h1>
Email:  <br/>
User id: 

3) Set the template and called methods

Open the page for edit, click the callback tab and for PHP Callback: set the class name and method (Dev_Affportal_Tools :: affportal).

For the basic information tab, at Links Template: add "custom_template"

That should be all.

Please watch this video for better explination if needed.