Getting Started — Dwoo PHP Template Engine

Learn how to get started with Dwoo PHP template engine. Includes basic usage, Template and Data objects, Compiler usage, and a blog loop example.

Running Dwoo

Dwoo at it's simplest

Here is the simplest way to output a Dwoo template.

<?php
 
// Include the main class, the rest will be automatically loaded
include 'path/to/Dwoo.php';
 
// Create the controller, it is reusable and can render multiple templates
$dwoo = new Dwoo();
 
// Create some data
$data = array('a'=>5, 'b'=>6);
 
// Output the result ...
$dwoo->output('path/to/index.tpl', $data);
// ... or get it to use it somewhere else
$dwoo->get('path/to/index.tpl', $data);

Using Template and Data objects

In this example we add the Dwoo_Data class that serves as a data container, and the Dwoo_Template_File class that represents a template file in your application.

<?php 
 
// Include the main class, the rest will be automatically loaded
include 'path/to/Dwoo.php';
 
// Create the controller, it is reusable and can render multiple templates
$dwoo = new Dwoo();
 
// Load a template file, this is reusable if you want to render multiple times the same template with different data
$tpl = new Dwoo_Template_File('path/to/index.tpl');
 
// Create a data set, this data set can be reused to render multiple templates if it contains enough data to fill them all
$data = new Dwoo_Data();
// Fill it with some data
$data->assign('foo', 'BAR');
$data->assign('bar', 'BAZ');
 
// Output the result ...
$dwoo->output($tpl, $data);
// ... or get it to use it somewhere else
$dwoo->get($tpl, $data);

Using a Compiler object

If you want to use custom filters, you need to instantiate a Dwoo_Compiler and add the filter to it.

<?php 
 
include 'path/to/Dwoo.php';
$dwoo = new Dwoo();
$tpl = new Dwoo_Template_File('path/to/index.tpl');
$data = array('a'=>5, 'b'=>6);
 
// Create the compiler instance
$compiler = new Dwoo_Compiler();
// Add a filter that is in one of the plugin directories
$compiler->addPreFilter('Filtername', true);
// .. Or a custom filter you made
$compiler->addPreFilter('Filter_Function_Name');
 
// Output the result and provide the compiler to use
$dwoo->output($tpl, $data, $compiler);

Using Dwoo with loops - Blog example

Let's assume you are looping over multiple articles of a blog that you want to display, here is what you can do to do it as lightly as possible :

You first have to create an "article.tpl" template file, the name doesn't matter really it's up to you, here is what goes in :

<div class="article">
<h1>{$title}</h1>
{$content}
<p class="footer">posted by {$author} on {date_format $date "%d/%m/%Y"}</p>
</div>

You will then use this template to render all the articles.

<?php 
 
include 'path/to/Dwoo.php';
 
$dwoo = new Dwoo();
// Load the "article" template
$tpl = new Dwoo_Template_File('path/to/article.tpl');
 
// Retrieve your data using whatever means you use
$articles = getMyArticles();
 
// Loop over them
foreach($articles as $article) {
// Output each article using their data (assuming it is an
// associative array containing "title", "content", "author"
// and "date" keys)
$dwoo->output($tpl, $article);
}