• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Template and code. how do I do it?

Laric

Active member
#1
I am trying to do an addon that essentially is a template and a bunch of code behind it to feed the template with data.

I just don't quite get how to do such an addon. I have made addons before that hooks into different places in xf. But not one that is 'on it's own'.

I want some code to execute when the template is rendered. But where do I start?

I am trying to add a ventrilo status to the sidebar.

"<xen:include template="laric_ventrilostatus">" should kind of trigger the code so the variables I return is available to the template right?

Anybody have a simple addon that does this? I tried to look at TaigaChat but that addon is alot more complex than what I need.
 

Shadab

Well-known member
#2
<xen:include template="laric_ventrilostatus"> should kind of trigger the code so the variables I return is available to the template right?
Nope. Templates are dumb. They don't know (and don't care) anything about fetching data; which is why you need to explicitly fetch all the data that a template needs, before rendering it.
 

Laric

Active member
#3
Nope. Templates are dumb. They don't know (and don't care) anything about fetching data; which is why you need to explicitly fetch all the data that a template needs, before rendering it.
But how do I do it? That is what I am trying to figure out. Should I use an event or a listener or how should I go about feeding data into my template?

Would love a simple example if you have the time.
 

Shadab

Well-known member
#4
Depends on where you would be displaying your rendered template.
I take it you are using a template hook to add your contents to a sidebar?
 

Laric

Active member
#5
Depends on where you would be displaying your rendered template.
I take it you are using a template hook to add your contents to a sidebar?
I was thinking of just using "<xen:include template="laric_ventrilostatus">" is more or less any template so the 'enduser' could decide where it goes on his forum.
 

Shadab

Well-known member
#6
Well, one quick way is to create an event listener for the "front_controller_pre_view" event.

If you expect the end-user to place the include tag in a 'container' template, modify the "$containerParams" variable. Otherwise, if you expect the tag to be placed in a 'content' template, modify the "$controllerResponse" variable. Here's some sample code:

PHP:
class Laric_Addon
{
	public static function frontControllerPreView(XenForo_FrontController $fc, XenForo_ControllerResponse_Abstract &$controllerResponse, XenForo_ViewRenderer_Abstract &$viewRenderer, array &$containerParams)
	{
		$status1 = 'Set all your variables here';
		$status2 = 'That would be used in your template';

		// Parameters available to content templates
		$controllerResponse->params += array(
			'status1' => $status1,
			'status2' => $status2,
		);

		// Parameters available to container templates
		$containerParams += array(
			'status1' => $status1,
			'status2' => $status2,
		);
	}
}
The callback for your event in this case would be: Laric_Addon :: frontControllerPreView
 

Laric

Active member
#7
I finally got it working.

But is there any way to limit it so it only actually runs when it is 'needed'. I mean it is no point for me to fill out all the variables etc when the template isn't even on the page.

Especially as my code is really expensive timewise.

I definetly need to figure out some kind of caching. My local dev setup went from 0.02 sec generations to 0.1 sec generations.
 

Shadab

Well-known member
#8
Especially as my code is really expensive timewise. I definitely need to figure out some kind of caching. My local dev setup went from 0.02 sec generations to 0.1 sec generations.
You can offload all your code to a cron entry; and run it at an interval of, say 5 minutes, or even more if the result of your processing doesn't change often. In your cron entry, save the result in xenforo's simple cache:
PHP:
XenForo_Application::setSimpleCacheData('your_cache_key', $serializedResult);
Then in your event listener, simply get the result from cache:
PHP:
$serializedResult = XenForo_Application::getSimpleCacheData('your_cache_key');
Note that '$serializedResult' will be false if the cache key doesn't exist.