How To Use Template Hooks

Well, you'd need to store the code you're injecting, surely? If I was to use a hook to put {xen:helper dump, $someVar}, that code would need storing somewhere outside the template so it's not overwritten during upgrade. If there's no file to store it in, it'd only make sense that it was database stored?
 
Well, you'd need to store the code you're injecting, surely? If I was to use a hook to put {xen:helper dump, $someVar}, that code would need storing somewhere outside the template so it's not overwritten during upgrade. If there's no file to store it in, it'd only make sense that it was database stored?
Not really.
They could change it direct in the template(ok they could store if, if you want to uninstall the add-on, the system would need to undo the change)
That's what the vb TMS is doing and that's what i'm hopping kier and mike are building for us.


for example:
original template "baz"
Code:
<div id="foo">
{$bar}
</div>

now you install a add-on which needs a change in baz (what the system would hopefully do while the add-on install)
search for
Code:
<div id="foo">
replace it with:
Code:
<div id="foo">
<div class="addonContainer">
{$myOutput}
</div>

The template system could search for this and replace it in the original template.
So it would happen only ONCE instead on runtime.

vBulletin have something similar ( http://www.vbulletin.com/forum/entry.php/2350-vB-4.0-Template-Merging-and-3-Way-Comparison ) but it's not made for add-on template changes:(
 
Wouldn't that cause a problem when you want to uninstall/disable an add-on, because it's basically just replaced the original code?
 
Wouldn't that cause a problem when you want to uninstall/disable an add-on, because it's basically just replaced the original code?
That's why i wrote
(ok they could store if, if you want to uninstall the add-on, the system would need to undo the change)
BUT
that's just the theory, i never checked how vB TMS is working, because if you made more changes, my way wouldn't work:D

addon1:
search for <div id="foo"> replace with <div id="foo">baz

addon2:
search for <div id="foo"> replace with <div id="foo">something completely different

would result in
<div id="foo">something completely differentbaz

so the uninstall wouldn't work:D
have really to check how vB TMS is doing this
 
Is there a mechanism in place to display errors if an add-on is not installed correctly, or some other kind of debug mode that might explain how the add-on is working?

I modified an existing add-on, as I wanted to use a template hook and a new template as opposed to modifying an existing template. I followed the second part of Kier's video, and I even have the filenames correct this time, but I can't get my modification to display.
 
I need to stop working on these add-ons so late...I found my stupid error. Completely forgot to add $template->getParams().
doh.gif


It's a wonder I can feed myself...
 
I'm try to follow this tutorial, but I get "Parse error: syntax error, unexpected '=', expecting T_STRING in /Users/gldtn/Sites/xenforo/library/GAB/Listener/Listener.php on line 3"

Code:
<?php

class= GAB_Listener_Listener
{
  public static funtion templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
  {
    if ($hookName == 'ad_below_top_breadcrumb')
    {
      $content .= $template->create('GAB_billboard');
      break;
    }
  }
}
 
The parse error is due to the = sign after class.
Code:
class= GAB_Listener_Listener

Ahh.. I totally missed it.. Thank! But now, nothing shows up, I've tried injecting raw html and everything, but nothing comes up anywhere :/

Anything I'm missing?

Code:
<?php

class GAB_Listener_Listener
{
  public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
  {
    if ($hookName == 'ad_below_top_breadcrumb')
    {
      $content .= $template->create('GAB_billboard');
      break;
    }
  }
}
 
Ahh.. I totally missed it.. Thank! But now, nothing shows up, I've tried injecting raw html and everything, but nothing comes up anywhere :/

Anything I'm missing?

Code:
<?php

class GAB_Listener_Listener
{
  public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
  {
    if ($hookName == 'ad_below_top_breadcrumb')
    {
      $content .= $template->create('GAB_billboard');
      break;
    }
  }
}

You're passing a variable called $contents into the function, and then modifying a variable called $content.
 
Thanks Arik, I got it to work, but I had to use return; because break; was giving me
Code:
Fatal error: Cannot break/continue 1 level in /home/cogumelo/public_html/comunidade/library/GAB/Listener/Listener.php on line 10

Also, I had to take this to a live site to see it working, because on my localhost it just would not do anything, not even give me an error with debug on!
 
Following the introduction of the mega footer here on XenForo.com, which I said was included using template hooks and requires no direct template edits, this video tutorial talks about template hooks, what they are and why you would want to use them.

It then goes on with a few examples to get you started.

Quick note, the method signature you copied out of your admin is slightly different than the released version.

You show:
PHP:
$hookName, &$content, array $hookParams, XenForo_Template_Abstract $template

The method signature that's showing in my version of XenForo looks like this:

PHP:
$hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template

Which accounts for why some people are having an issue getting it working. ;)
 
That's true, but provided all the variable names marry up with each other, it doesn't matter what they are called at all.
 
That's true, but provided all the variable names marry up with each other, it doesn't matter what they are called at all.

Yes. But, in the video you advocate copying the function's signature. That means anyone copying you verbatim will get an error. I've helped a few people who have had two different variable names. Just trying to be proactive.
 
Yes. But, in the video you advocate copying the function's signature. That means anyone copying you verbatim will get an error. I've helped a few people who have had two different variable names. Just trying to be proactive.
I'm not sure I understand what you're saying - if you copy the function signature from your control panel and write your function using the variables you copied, it will work.

On the other hand, if you copy my code in the example I posted, why would you copy the function signature from your control panel?

I'm genuinely confused, not trying to make a point or anything.
 
I'm not sure I understand what you're saying - if you copy the function signature from your control panel and write your function using the variables you copied, it will work.

On the other hand, if you copy my code in the example I posted, why would you copy the function signature from your control panel?

I'm genuinely confused, not trying to make a point or anything.

Some users would copy the signature but follow your instructions instead and enter the "content" variable instead of using the variable in the signature they copied.
 
Some users would copy the signature but follow your instructions instead and enter the "content" variable instead of using the variable in the signature they copied.
Source code changes?
SINCE WHEN!
 
Source code changes?
SINCE WHEN!

There is just an extra "s" in the signature code for template hook in 1.0. However, in the video the signature for the template hook doesnt have an "s". So, it's "contents" right now and not "content".
 
Top Bottom