PHPackages                             paudebau/h2o-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. paudebau/h2o-php

ActiveLibrary

paudebau/h2o-php
================

A beautiful template engine for PHP in Django style

02.7k2PHP

Since Apr 12Pushed 13y agoCompare

[ Source](https://github.com/paudebau/h2o-php)[ Packagist](https://packagist.org/packages/paudebau/h2o-php)[ RSS](/packages/paudebau-h2o-php/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

H2O template markup
===================

[](#h2o-template-markup)

Being a martial arts fan, I borrow a quote.

H2O template
------------

[](#h2o-template)

H2O is markup language for PHP that has taken a lot of inspiration from Django.

**Features**

- Readable and human-friendly syntax.
- Easy to use and maintain
- Encourages reuse in templates by allowing template inclusion and inheritance.
- Highly extensible through filters, tags, and template extensions.
- Includes a rich set of filters and tags for string formatting, HTML helpers and internationalization support.

Requirement
-----------

[](#requirement)

- PHP 5.1 +

News
----

[](#news)

- version 0.4
    1. **Breaking changes** autoescape is now turned on by default
    2. Improved searchpath and file loading handling
    3. Improved handling on PHP overloaded objects
    4. Plenty of bug fixes
- version 0.3
    1. Support internationalized templates and translation parsing toolkit
    2. Performance optimization on context lookup
    3. Fixed operator parsing

Getting started
---------------

[](#getting-started)

### Getting h2o

[](#getting-h2o)

Download

[![](http://github.com/images/modules/download/zip.png)](http://code.google.com/p/h2o-template/downloads)

With Git

`git clone http://github.com/speedmax/h2o-php.git`

With SVN

`svn checkout http://h2o-template.googlecode.com/svn/trunk/ h2o`

### Installation

[](#installation)

1. Download and extract h2o into your project path or your php include path

    Sample file structure setup

    ```
    myawesome_app/
        index.php
        templates/
          index.html
        h2o/

    ```
2. Use `require 'h2o/h2o.php'` in your php files to include the h2o library.
3. Below is a basic code sample to get your project going.
4. Check out the *\\example* and *\\specs* dirs to see some of h2o's more interesting features in action.

*templates/index.html*

```

    Hello world

        Greetings {{ name }}

```

*index.php*

```

```

Useful links
------------

[](#useful-links)

- Please submit patches or bug reports to our [lighthouse bug tracker](http://idealian.lighthouseapp.com/projects/11041-h2o-template-language).
- Check out our [Google group](http://groups.google.com/group/h2o-template-php) for h2o-related discussion.

Syntax explanation
------------------

[](#syntax-explanation)

variable
--------

[](#variable)

`{{ variable }}`

Use dot (.) to access attributes of a variable

#### variable lookup order

[](#variable-lookup-order)

1. key of an associative array
2. array-index
3. object attribute
4. object method call

**Example**

*in your template*

```
{{ person.name }}

```

*in php*

```

```

Let's say that you have assigned the value `Peter Jackson` to a 'person' variable in your php script. The following variable tag will print out `Peter Jackson`.

Filters
-------

[](#filters)

Filters are variable modifiers that manipulate or format the value of a variable. A filter usually looks like this `{{ person.name|capitalize }}`, a pipe ( | ) character after a variable, plus a filter name, will cause H2O to apply the filter.

**Filter chaining**

[![filter chaining](https://camo.githubusercontent.com/6901a318a7dda47cb014f804d1224dfb24b7cafa57d65a4e0aeb46dbc671adf7/687474703a2f2f77696b692e73686f706966792e636f6d2f75706c6f61642f382f38632f46696c746572636861696e2e6a7067)](https://camo.githubusercontent.com/6901a318a7dda47cb014f804d1224dfb24b7cafa57d65a4e0aeb46dbc671adf7/687474703a2f2f77696b692e73686f706966792e636f6d2f75706c6f61642f382f38632f46696c746572636861696e2e6a7067)
Let me borrow the image from liquid template

You can chain multiple filters together and use a pipe ( | ) character to separate them. `{{ document.body|escape|nl2br }}`

**Filter arguments**
Filters can accept arguments. For example: `{{ document.description|truncate 20 }}`will display the first 20 characters of the document's description.

Moreover, there are cases where you might want to pass in multiple arguments. Use commas ( , ) to separate them: `{{ person.bio|truncate 20, "..." }}`

**Filter named arguments**
h2o uses colons ( : ) for named arguments. These allow you to build 'optional argument' arrays.

`{{ '/images/logo.png' | image_tag width:450, height:250, alt:"company logo" }}`

The above code translated to php will be like the below snippet, which resembles what happens internally:

```

```

Note: Difference with Django, Smarty H2o does not use the colon ( : ) character to separate arguments for readability reasons, H2o uses the comma ( , ) which is more logical.

Tag
---

[](#tag)

`{% tag %}`

Tags are very powerful, as they control the logical flow and structure of a template, There are inline tags `{% inline_tag %}` or tags that requires a close tag. For example: `{% if condition %} ... {% endif %}`

### The "if" tag

[](#the-if-tag)

`if` tags evaluate either a variable or a simple expression. If the result of the `if`expression is *true*, then the contents of the `if` block will be allowed to render.

```
{% if person.is_adult %}
    You are old enough.
{% else %}
    sorry, you are too young for that.
{% endif %}

```

### The "for" tag

[](#the-for-tag)

`for` tags allow iteratation over an array of items.

```
{% for task in tasks %}
    {{ task }}
{% endfor %}

```

The above snippet will print out each "task" in the "tasks" array.

Template inheritance
--------------------

[](#template-inheritance)

H2o supports template inheritance. Inheritance allows you to factor out a lot of common code that would otherwise be duplicated across most of your templates.

Template inheritance is implemented using the `block` and `extends` tags, with child templates *extending* their parent templates. **Word of Caution:**

- H2o templates only support single inheritance (just like PHP!), and currently do not support deep inheritance chains.

Quote from the Django docs:

> ... a base skeleton template that contains all the common elements of your site and defines blocks that child templates can override.

*Example:*

*base.html* - defines the base structure of our pages.

```

 {%block title %}This is a page title {% endblock %}

   {% block content%}
        Page title
        H2O template inheritance is a powerful tool
   {% endblock %}

   {% block sidebar %}{% endblock %}

```

As you can see, the base template is a typical web page using a two column layout. We defined two blocks (`content` and `sidebar`) and HTML code common across all of our pages.

*page.html* - defines a page-specific template.

```
{% extends 'base.html' %}

{% block content %}
     extended page
     Body of extended page
{% endblock %}

{% block sidebar %}
    Sidebar contains use links.
{% endblock %}

```

The `page.html` extends `base.html`, allowing us to override any block previously defined in `base.html`.

Below is an excellent article about template inheritance in Django. If you wish to understand H2o's template-inheritance system, this would be a great spot to start, since H2o's template-inheritance system is strongly influenced by Django's.

[Power of inheritance](http://www2.jeffcroft.com/blog/2006/feb/25/django-templates-the-power-of-inheritance/) is a very good blog post explaining inheritance

*Tips*

- If you have found that you have several common elements inside the same template, it may be a good idea to put that portion of the template inside a `block` in a base template.
- `block` give you a hook, which is useful, since these can help with javascript and css too.
- When defining a block use a short and distinctive name

### Configuration

[](#configuration)

There are a range of options for configuring the template engine.

```

```

#### Loader

[](#loader)

The name of the loader or an instance of H2o\_Loader

**Use file loader \[default\]**

`$template = new H2o('index.html', array('loader'=>'file');`

**Advanced setup** $loader ); ?&gt;

**Use dictionary loader**

If you want to load templates from resources other than files, then this will be your friend. H2o uses `dict_loader()` for testing.

```
