Extending Dwoo

Dwoo can be extended in many ways; you can add extra blocks, functions and filters*. You can even extend the Core itself for your application.


A global variable is like any other template variable, except that it’s available in all templates.

Actually globals are defined in the Core class and cannot be modified directly. This feature is now available from Dwoo 1.3.2, please upgrade your dependency!

Extending Core

class MyTemplate extends Dwoo_Core {


Creating custom Plugin(s)

In addition to providing you with an abundance of built-in plugins, Dwoo also allows you to create your own. These plugins may be implemented as user-defined functions or standalone classes, and can be either manually added at run-time via Dwoo’s addPlugin() method or automatically loaded by the Dwoo autoloader.

To illustrate, consider the following script, which sets up a simple plugin to manipulate email addresses:

// simple plugin
function fix_address(Dwoo_Core $core, $str) {
    return str_replace(
      array('@', '.', '-'), 
      array(' at ', ' dot ', ' dash '), 

// create Core object
$dwoo = new Dwoo_Core();

// read template file
$tpl = new Dwoo_Template_File('tmpl/plugin.tpl');

// add custom plugin
$dwoo->addPlugin('email_safe', 'fix_address');

// set sample string
$data['string']= 'vikram@example.com';

// interpolate values into template
// send interpolated result to output device
echo $dwoo->get($tpl, $data);

Here, the fix_address() function replaces common email address characters with human-readable words, in order to make it harder for spammers to harvest these addresses from Web pages. The addPlugin() method takes care of adding this function to Dwoo’s plugin list, specifying both the local plugin name and the function callback.

Here’s how you’d use it in a template:


If you want add more than one custom plugin, you can specify to the loader a custom directory to search for, such as:

$dwoo->getLoader()->addDirectory(__DIR__ . 'Views/plugins/');