How Django template work
In Django template you can’t mix programming logic into template, and this is by the design, because basically template is meant to express presentation. But of course, Django template provide something called tags which function similarly to some programming constructs. Now let’s go through some real example how Django template work.
In Django template we use {{ variable }} to express variable.
To loop over array or list we use for tag
And to simulate selection, Django template provide if tag
Template inheritance
In Django template, there is feature called template inheritance. This is the most powerful part of Django template system. For example, just say we have file base.html
Then we can reuse base.html by creating another file, say index.html.
{% extends 'base.html' %}
{% block title %} This is index page {% endblock %}
{% block content %} This is content of index page {% endblock %}That’s it, index.html will have base.html structure and only override some of value that has been declared as block in Django template.
For more information about Django template, visit Django template documentation.
You fall in love with Django template, but you use PHP for your project. Don’t be disappointed because there is PHP project that port Django template into PHP world. It’s called H2O.
Now, we start our H2O introduction.
H2O template
H2O is markup language for PHP that has taken a lot of inspiration from Django.
This is some of H2O features :
- Readable, human friendly syntax
- Easy to use and maintain
- Encourages reuse in templates by template inclusion and inheritance
- Highly extensible through filters, tags and template extensions
- Bundled rich set of filters and tags for string formatting, HTML helpers and internationalization
So let’s begin our tutorial using Django template in PHP.
Download and extract h2o file, and create this structure directory
project/
index.php
h2o.php
templates/
index.html
h2o/
...
Create index.html and write this following code
Then create index.php, and write this code
<?php require 'h2o.php'; $template = new H2o('templates/index.html', array( 'cache' => false, 'cache_dir' => dirname(__FILE__) )); # render your page echo $template->render(array( 'title' => 'Hello Django template', 'body' => 'Hello world' )); echo $h2o->render(array( 'title' => 'title of a page', 'body' => 'Hello world' )); ?>
That’s it. You have create simple working Django template style in PHP.
This post only introduction for using Django template in PHP project, if you really have interest on Django template and H2O please go to the following website.
Related posts:
- Ultimate Collection of PHP Libraries
- 101 PHP Tutorials for PHP Programmer Wannabe
- Codeigniter tutorial – To do list application
- 20 Great PHP Libraries You Need to Know
- 4 Simple PHP RSS Parser
Tags: PHP
March 8th, 2009 | Comments(2)

Thanks for the notes on h2o. I love it. Great system. You can also install the django template syntax for either django or h2o code coloring in Coda.
http://weblog.bignerdranch.com/?p=49
Comment by Chuck — March 31, 2009 @ 8:48 pm
@Chuck Thanks for the information. I don’t use Coda but I really think it will help someone out there.
Comment by Gilang Chandrasa — March 31, 2009 @ 9:35 pm