This extension adds the Twig Template Engine to MediaWiki. This extension Does nothing on its own and is meant to be a dependency of other extensions.
- Add the extension into
extensions/Twiggy
in your MediaWiki installation. - Add the Twiggy
composer.json
tocomposer.local.json
:
{
"extra": {
"merge-plugin": {
"include": [
...,
"extensions/Twiggy/composer.json"
]
}
}
}
- Run
composer update
. - Add this code block to the bottom of the LocalSettings.php file:
wfLoadExtension('Twiggy');
- Add
Twiggy
as a required extension inextension.json
:
{
...
"requires": {
"extensions": {
"Twiggy": ">= 0.0.1"
},
"MediaWiki": ">= 1.31.0"
}
...
}
- Add a hook for
SpecialPageBeforeExecute
:
{
"Hooks": {
"SpecialPageBeforeExecute": "MyExtensionHooks::onSpecialPageBeforeExecute"
}
}
- Define the hook to register the template location:
public static function onSpecialPageBeforeExecute( SpecialPage $special, $subPage ) {
$twig = MediaWikiServices::getInstance()->getService( 'TwiggyService' );
$twig->setTemplateLocation('MyExtensionName', __DIR__ . '/../resources/templates');
}
- Example Special Page Usage:
public function execute($subpage) {
...
$twig = MediaWikiServices::getInstance()->getService( 'TwiggyService' );
$template = $twig->load('@MyExtensionName/template.twig');
$this->output->addHtml($template->render(['title' => $title, 'types' => $types]));
}
- Example Template
template.twig
<h1>{{ wfMessage('title_language_key', 'plain') }}</h1>
<ul>
{% for id, type in types %}
<li id="type_{{ id }}>{{ type.title }}</li>
{% endfor %}
</ul>
Twiggy has a pass-through function for wfMessage()
support. In templates all arrow methods are converted to dot syntax.
{{ wfMessage(string $msg).plain() }}