Question on Template Hook Compilation

Shadab

Well-known member
Here's some sample code I'm using to test (and understand) how template hooks are compiled to php statements. I'm running this code in a separate script, outside of the XenForo/MVC framework.
PHP:
$text = <<<TEMPLATE
<xen:hook name="template_title.foo" params="foo:bar">
	My sample text.
</xen:hook>
TEMPLATE;

$compiler = new XenForo_Template_Compiler($text);
$output = $compiler->compile();

echo $output;
The compiled php code is:
Code:
$__output = '';
$__compilerVar1 = '';
$__compilerVar1 .= '
	My sample text.
';
$__output .= $this->callTemplateHook('template_title.foo', $__compilerVar1, 'foo:bar');
unset($__compilerVar1);
Which would definitely result in an error, since the 3rd argument for callTemplateHook is expecting an array. Is there any special syntax for passing hardcoded parameters (key-value pairs) to a hook?
 
Use {xen:array} to pass params.

{xen:array "name={$value}"} => array('name' => $value)
 
That's awesome! I didn't realize arrays were supported in this way inside templates.
Thanks. :)

Code:
<xen:hook name="template_title.foo" params="{xen:array 'key1=value1', 'key2=value2'}">
	My sample text.
</xen:hook>
 
It was mentioned in the release announcement for Beta 3:
http://xenforo.com/community/threads/xenforo-1-0-0-beta-3-released.7891/

Help for Add-on Developers
A framework for 'template events' now exists, although the default templates do not make use of it yet. This will eventually allow add-ons to add their own content to templates in a programatic manner.


These hooks seem quite powerful. Instead of being just a placeholder for addons to append text; they act like "blocks". You can append, prepend, remove or even completely replace the existing markup enclosed in a xen:hook tag.
 
Where can we look to find out more about template events?
You can look directly in the source:
  • /library/XenForo/Template/Compiler/Tag/Hook.php (how it's compiled; not really relevant for us)
  • /library/XenForo/Template/Abstract.php (how the hook is called internally; line ~160)
You won't find any example of a template hook in the Beta 3 source. The mechanism for calling hooks is in place; but it's not used anywhere, yet. Here's some sample code:

» Template
Code:
<xen:hook name="helloworld_view">
	<p>{xen:phrase hello_world}</p>
</xen:hook>

» Event Listener
PHP:
public static function template_hook($name, &$contents, array $params, XenForo_Template_Abstract $template)
{
	// Choose the hook you want to manipulate
	if ($name === 'helloworld_view')
	{
		// Change the value of $contents in any way you want
		$contents .= new XenForo_Phrase('admin_control_panel');

		// Any parameters you pass to the hook via params="" attribute
		// are available in the $params array.
		$contents .= Zend_Debug::dump($params, 'Parameters', false);
	}
}
 
[..] comments in particular areas within a hook block. :)
An indication of how we're going to see template hooks being used in stock templates, I suppose. :)

How practical would be implicit hooks at template level? ie, immediately after render()'ing the template, the hook will be called with template title as hook name and all the template parameters passed along.
 
A post-render event? Not particularly hard, though that's probably too high level to make any useful changes. You're only going to get the final output, and that would be something like this entire thread view page. As template includes are resolved at compile time, the individual post templates aren't evaluated separately.

The params part of the template hook tag allows us to pass especially pertinent data through, in a consistent position. (You can of course access all the params in the template at this point, via the last argument to the event. Though this may not contain exactly what you expect.)
 
[...] though that's probably too high level to make any useful changes. You're only going to get the final output, and that would be something like this entire thread view page. As template includes are resolved at compile time, the individual post templates aren't evaluated separately.
Ah! That explains why the hooks for included templates like header, footer, sidebar_*, etc. didn't run when I was testing this approach. Seems like explicitly defining hooks is the only solution. Looking forward to seeing how they are implemented in default templates.
 
Top Bottom