YesNoOk
avatar

How template hooks work (Read 4304 times)

Started by Nightwish, July 09, 2012, 16:50:42

0 Members and 1 Guest are viewing this.
Share this topic:
How template hooks work
#1
Posted by Nightwish July 09, 2012, 16:50:42
Template hooks is a mechanism that allows plugins to insert generated output at specific locations. Plugins can register template fragments, which are chainable so that multiple plugins can populate the same hook position.

Lets assume, a plugin wants to extend the user block in the sidebar (the small block where the member's avatar, nick name and stats appear). First, the plugin must use a code hook (not discussed here, but the method is very similar to SMF) to generate the output. For example, a plugin could hook load_userdata to extend the $user_info[] array with its own data.

After that, the plugin must register the template fragments to extend the user block in the sidebar. Since this user block is only visible on the board index, it's best to use the boardindex hook for this purpose. So we hook into boardindex with a small function
Code: (php)
class MyPlugin {
  [...]
  public static function boardindex_hook() // runs only in board index
  {
    // add our plugin directory to the list of directories to search for templates.
    EoS_Smarty::addTemplateDir(self::$_dir);  // allow template loading from our plugin's home directory
    EoS_Smarty::getConfigInstance()->registerHookTemplate('sidebar_userblock', 'myplugin_userblock');
  }
}
We must then write a new template myplugin_userblock.tpl and store it in our plugin's root directory (what $_dir points to in the example above). Note that the extension .tpl is always omitted when working with templates, it's assumed to be the default filename extension for all templates.
Code: (php)
<div class="flat_container mediumpadding">
  {$U.mydata}    {* output what we have added to $user_info *}
</div>
If all goes well, our output should appear at the bottom of the user block in the boardindex side bar.

So basically, template hooks allow to extend existing templates without the need to modify them. Right now, there are only a few template hooks and they only work in the new smarty templates, but the system will be gradually extended over time to allow for a flexible plugin architecture and little need to modify existing templates.
Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to a single instruction that doesn't work.
My SMF-based forum fork
2 Like It  | 2 Useful 
#2
Re: How template hooks work |
July 10, 2012, 03:06:55
Very interesting! I'm interested on this myself (plugin system + hooks), for sure watching how exactly you do this will be a tremendous help for me :)
__
Sin talento no busques grandeza, porque nunca la vas a obtener.
1 Like It 
#3
Re: How template hooks work |
July 11, 2012, 12:10:58
Very interesting! I'm interested on this myself (plugin system + hooks), for sure watching how exactly you do this will be a tremendous help for me :)
Template hooks are a pretty big deal for mod- and plugin authors and are harder to implement than I thought :) I've been looking at various implementations in other forums (vB, Invision etc.) and they all have their own problems and flaws. I guess, implementing a theme hook system that could fit any purpose and eliminate all theme edits is pretty much impossible. So the goal is to create something that works for most cases.

Also, it's important to watch for performance impacts of such hooks. It's not possible to avoid them completely, but I want to keep them as minimal as possible.
__
Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to a single instruction that doesn't work.
My SMF-based forum fork
1 Like It