For a MediaWiki modification I'm working on I added a hook to the
SkinTemplate.php file and specifically the outputPage function. As the
name implies I inserted it before the execute() function is called. The
code now looks like this:
$tpl->set( 'sidebar', $this->buildSidebar() );
$tpl->set( 'nav_urls', $this->buildNavUrls() );
// Hook that allows last minute additions to the
// template, before it is executed.
wfRunHooks( 'BeforeTemplateExecution', array( &$tpl ) );
// execute template
wfProfileIn( "$fname-execute" );
$res = $tpl->execute();
wfProfileOut( "$fname-execute" );
This allows me to add a function to the hook in LocalSettings.php the
normal way:
$wgHooks['BeforeTemplateExecution'][] = 'fnExtendTemplate';
The function looks like this:
function fnExtendTemplate( &$tpl ) {
$tpl->set( 'mainmenu', buildMainMenu() );
return true;
}
And the buildMainMenu function, which is also added to the
LocalSettings.php file:
function buildMainMenu() {
return "Hello World!";
}
With these functions I can simply add the following line to the Skin file:
<?php print($this->data['mainmenu']); ?>
I made this because I have a mainmenu and a submenu on the site I'm
working on, located on different places in the HTML part of the site.
The buildMainMenu and the buildSubMenu function both call a function
that gets the content of the menu structure (similar to
MediaWiki:Sidebar) and parses it. buildMainMenu and buildSubMenu then
render their part of the menu, depending on the page title.
This hook is useful to me, because it lets me add elements to the
template in a quick, clean and simple way. Is this also useful to the
developers community?
Boris
Show replies by date