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.
1 2 3 4 5 6 | <html> <head><title>{{ page.title }}</title></head> <body> {{ page.body }} </body> </html> |
To loop over array or list we use for tag
1 2 3 4 5 6 7 8 9 | <html> <head><title>{{ page.title }}</title></head> <body> {% for news in news_list %} <h2>{{ news.title }}</h2> <p>{{ news.content }}</p> {% endfor %} </body> </html> |
And to simulate selection, Django template provide if tag
1 2 3 4 5 6 7 8 | <html> <head><title>{{ page.title }}</title></head> <body> {% if message %} {{ message }} {% endif %} </body> </html> |
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
1 2 3 4 5 6 7 8 9 10 | <html> <head> <title>{% block title %}Base{% endblock %}</title> </head> <body> {% block content %} {% endblock %} </body> </html> |
Then we can reuse base.html by creating another file, say index.html.
1 2 3 4 5 | {% 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
1 2 3 4 5 6 | <html> <head><title>{{ page.title }}</title></head> <body> {{ page.body }} </body> </html> |
Then create index.php, and write this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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:
- 101 PHP Tutorials for PHP Programmer Wannabe
- Ultimate Collection of PHP Libraries
- Getting Started With Django-CMS
- A Quick Tutorial on Django MPTT
- Codeigniter tutorial – To do list application
Tags: PHP
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
@Chuck Thanks for the information. I don’t use Coda but I really think it will help someone out there.