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

Question on Template Hook Compilation

Discussion in 'XenForo Development Discussions' started by Shadab, Nov 24, 2010.

  1. Shadab

    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.
    $text = <<<TEMPLATE
    <xen:hook name="template_title.foo" params="foo:bar">
        My sample text.

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

    The compiled php code is:
    $__output = '';
    $__compilerVar1 = '';
    $__compilerVar1 .= '
    	My sample text.
    $__output .= $this->callTemplateHook('template_title.foo', $__compilerVar1, 'foo:bar');
    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?
  2. Mike

    Mike XenForo Developer Staff Member

    Use {xen:array} to pass params.

    {xen:array "name={$value}"} => array('name' => $value)
    Shadab likes this.
  3. Shadab

    Shadab Well-Known Member

    That's awesome! I didn't realize arrays were supported in this way inside templates.
    Thanks. :)

    <xen:hook name="template_title.foo" params="{xen:array 'key1=value1', 'key2=value2'}">
    	My sample text.
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    Wait, when were xen:hooks introduced?!
  5. ragtek

    ragtek Guest

  6. Shadab

    Shadab Well-Known Member

    It was mentioned in the release announcement for Beta 3:

    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.
    Darkimmortal and Kier like this.
  7. Mike

    Mike XenForo Developer Staff Member

    ...or manipulate. Like if we place comments in particular areas within a hook block. :)
    Darkimmortal likes this.
  8. Jaxel

    Jaxel Well-Known Member

    Where can we look to find out more about template events?
  9. Shadab

    Shadab Well-Known Member

    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
    <xen:hook name="helloworld_view">
    	<p>{xen:phrase hello_world}</p>
    » Event Listener
    public static function template_hook($name, &$contents, array $paramsXenForo_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);
  10. Shadab

    Shadab Well-Known Member

    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.
  11. Mike

    Mike XenForo Developer Staff Member

    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.)
  12. Shadab

    Shadab Well-Known Member

    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.

Share This Page