ExtendingDwoo
Un article de Dwoo Docs.
[edit] Extending Dwoo
Here is a simplified version of the Dwoo wrapper class I use in my CMS, it uses some nice features that you might be interested in.
This class has several purposes :
- Allowing the creation of custom templates for a different language
- Allowing you to add custom instructions to the compiler, but without loading it until it's necessary, as it is a huge file and class it takes php a few milliseconds to load it so avoid it if you can
- Allowing you to add $dwoo.XXX constants to be used in all of your templates
<?php
class MyTemplate extends Dwoo
{
// stores a dwoo instance
protected static $instance;
// stores a compiler instance
protected static $cmpInstance;
// singleton accessor, use it to access this object if you need to call specific Dwoo functions
public static function getInstance()
{
// if it's not set, we create an object and set the default compiler factory function to the custom one
// you have to change "MyTemplate" by this class's name if you change it or it will not work
if(self::$instance === null)
{
self::$instance = new self();
self::$instance->setDefaultCompilerFactory('file', array('MyTemplate','compilerFactory'));
}
return self::$instance;
}
// creates a template object and does some automated processing before doing so
public static function createTemplate($name, $cache = null, $cacheId = null)
{
// if we are in development mode, disable the cache to prevent it from
// hiding changes made to the php code
if(defined('DEV_MODE'))
$cache = 0;
// for example you could also tweak the cache time globally for your application here..
// let's say you put a cache time of 1 2 or 3 depending on the template types in your code
// and then here you multiply it by 60 that 1 becomes 1 minutes (as Dwoo takes cache time in seconds)
// or you could even do if($cache == 2) { $cache = 533; } ... the point is that it allows you to
// change it in one place for the entire application
// we automatically append the language to the cache id if it's not empty,
// so that every page is saved in a different cache file for each language.
// you should of course replace User::$lang everywhere in this class by some
// other variable of yours that represents the language
if(empty($cacheId) === false)
$cacheId .= User::$lang;
// set the path to your template directory
$path = '/home/moo/mysite.com/templates/';
// look if there is a custom template for the current site language
if(file_exists($path . $name . '-' . User::$lang . '.html'))
{
$file = $path . $name . '-' . User::$lang . '.html';
}
// if not then look if the default template exists
elseif(file_exists($path . $name . '.html'))
{
$file = $path . $name . '.html';
}
// still not found, then throw an exception
else
{
throw new Exception('Unable to load template <em>'.$name.'</em> from <em>'.$path.'</em>', E_USER_ERROR);
}
// return the template object
return new Dwoo_Template_File($file, $cache, $cacheId);
}
// this function allows you to retrieve a template output
public static function fetch(Dwoo_Template_File $tpl, array $data, $customCompiler = null)
{
return self::getInstance()->get($tpl, $data, $customCompiler);
}
// this function overrides the default Dwoo one to add global values accessible through {$dwoo.VALUE} in the templates
protected function initGlobals(Dwoo_ITemplate $tpl)
{
// call the parent function to allow Dwoo to fill its default values
parent::initGlobals($tpl);
// then add yours to the array, here we set the language as an example
$this->globals['LANG'] = User::$lang;
}
// provides a custom compiler object when a template needs to be compiled
public static function compilerFactory()
{
if(self::$cmpInstance === null)
{
if(class_exists('Dwoo_Compiler', false) === false)
include 'Dwoo/Compiler.php';
self::$cmpInstance = Dwoo_Compiler::compilerFactory();
// here we set custom settings to the compiler, for example adding a PreProcessor
self::$cmpInstance->addPreProcessor('myPreProcessorFunction');
}
return self::$cmpInstance;
}
}
To use this class here's what you do basically when you want to build a template :
<?php
// let's assume you want to output an article with an id of two, we must add that
// id to the cache identifier so that every article has a unique cache file
// we also set the cache to 600 so it is rendered only once per 10 minutes
// assuming User::$lang is set to "fr", the template name "article" will make the function
// look for article-fr.html and if it does not exist it will look for article.html
// that way you can have custom text in french in the "-fr" template, that you do not want
// in the default template because your whole site is in english
$template = MyTemplate::createTemplate('article', 600, 'article'.$id);
// then you check if it's cached already, if it is you can output it right away no need
// to fetch any data from the database
if(MyTemplate::getInstance()->isCached($template))
{
// we assign a blank array as the data
$data = array();
} else {
// the article was not cached so we retrieve its data from the database
$article = mysql_query('...');
$data = mysql_fetch_assoc($article);
}
// finally we output it
echo MyTemplate::fetch($template, $data);
?>
[edit] Using the Include Plugin
If you decide to use the include plugin in your template you will need to add an extra method to the above class above to get it to work.
public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null){
// if we are in development mode, disable the cache to prevent it from
// hiding changes made to the php code
if(defined('DEV_MODE')){
$cache = 0;
}
// we automatically append the language to the cache id if it's not empty,
// so that every page is saved in a different cache file for each language.
// you should of course replace User::$lang everywhere in this class by some
// other variable of yours that represents the language
if(empty($cacheId) === false){
$cacheId .= User::$lang;
}
// set the path to your template directory
$path = '/home/moo/mysite.com/templates/';
// look if there is a custom template for the current site language
if(file_exists($path . $resourceId . '-' . User::$lang . '.html'))
{
$file = $path . $resourceId . '-' . User::$lang . '.html';
}
// if not then look if the default template exists
elseif(file_exists($path . $resourceId . '.html'))
{
$file = $path . $resourceId . '.html';
}
// still not found, then throw an exception
else
{
throw new Exception('Unable to load template <em>'.$resourceId.'</em> from <em>'.$path.'</em>', E_USER_ERROR);
}
// return the template object
return parent::templateFactory($resourceName, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate);
}




