Manual:QuickTemplate
- Maybe you were looking for Help:Templates instead?
The QuickTemplate class, defined in $IP/includes/SkinTemplate.php, allows to separate front-end user interface (UI) from backend stuff. It is currently—quite unfortunately—used by many skins. While skins shouldn't use this class, it certainly can be useful in some situations.
The source code documentation states that the QuickTemplate class is a [g]eneric wrapper for template functions, with interface compatible with what we use of PHPTAL 0.7.
Contents |
[edit] Historical background
Ages ago, MediaWiki's skins weren't purely PHP, they used to require PHPTal. When Brion Vibber rewrote the skin system to remove PHPTal dependency, SkinTemplate and QuickTemplate classes were introduced.[1]
[edit] Using QuickTemplate in extensions
Let's say that you're writing a new special page with a pretty UI and all that. It can prove quite difficult to do that with MediaWiki's Xml and Html classes. It is possible to use QuickTemplate in such cases.
| Remember to escape user-submitted input! Visit Security for developers for information on how to do this. |
[edit] MySpecialPage.php
You need to register the template class with MediaWiki's autoloader in your extension's setup file.
$dir = dirname( __FILE__ ) . '/'; $wgAutoloadClasses['MySpecialPageTemplate'] = $dir . 'MySpecialPageTemplate.php';
[edit] MySpecialPage.body.php
<?php /** * Main class for MySpecialPage example extension. * * @file * @ingroup Extensions */ class SpecialMySpecialPage extends SpecialPage { private $userName; // private class variable for storing the current user's username /** * Constructor -- set up the new special page */ public function __construct() { global $wgUser; $this->userName = $wgUser->getName(); // Define the private variable parent::__construct( 'MySpecialPage' ); } /** * Show the special page * * @param $par Mixed: parameter passed to the special page or null */ public function execute( $par ) { global $wgOut, $wgArticlePath; // Maybe check for user block/DB lock state and permissions here... // It's up to you, really. // Set page title etc. stuff $this->setHeaders(); // Begin actual template stuff $template = new MySpecialPageTemplate(); // Here we set the template variable 'foo', which in this case is a // string ($wgArticlePath with '$1' being replaced by 'Foo') $template->set( 'foo', str_replace( '$1', 'Foo', $wgArticlePath ) ); // $this is SpecialMySpecialPage object $template->setRef( 'mySpecialClass', $this ); // Template variable 'randomKey' is a randomly generated method that // the getRandomKey() method here defines for us. // getRandomKey() is a private class method, so this is pretty much // the only way we can use it outside this file/class $template->set( 'randomKey', $this->getRandomKey() ); // In the template class, all the variables we define here can be // accessed by using $this->data['variable_name'] // et voilà! $wgOut->addTemplate( $template ); } /** * Gets a randomly generated key. * * @return String */ private function getRandomKey() { $key = md5( 'superRandomString-' . $this->userName ); return $key; } }
[edit] MySpecialPageTemplate.php
<?php /** * @file * @ingroup Templates */ if( !defined( 'MEDIAWIKI' ) ) { die( -1 ); } /** * HTML template for Special:MySpecialPage * @ingroup Templates */ class MySpecialPageTemplate extends QuickTemplate { /** * Main processing is done here. * You can, should and want to use functions like wfMsg() here, because * QuickTemplate's own methods aren't that good. */ public function execute() { ?> <div id="MySpecialPageTemplate"> Your randomly generated secret key is: <?php echo $this->data['randomKey'] ?> If you need to generate a new key, you can do so <a href="<?php echo $this->data['mySpecialClass']->getTitle()->escapeFullURL() ?>">here</a> </div> <?php } // execute() } // class
We can use $this->data['mySpecialClass']->getTitle()->escapeFullURL() in the template class, because $this->data['mySpecialClass'] is a reference to the SpecialMySpecialPage object which extends the SpecialPage class (and SpecialPage class has the getTitle() method, which in turn returns a Title object, and the escapeFullURL() is a Title method).