PHPackages                             blesta/h2o - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. blesta/h2o

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

blesta/h2o
==========

H2O is a markup language for PHP

0.8.0(1y ago)1643↓33.3%1MITPHPPHP &gt;=7.1

Since Oct 25Pushed 1y ago5 watchersCompare

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

READMEChangelogDependencies (1)Versions (10)Used By (1)

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

[](#h2o-template-markup)

Being a martial art fan, I burrow a quote.

H2o template
------------

[](#h2o-template)

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

- Readable and human friendly syntax.
- Easy to use and maintain
- Encourage 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.

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 pasing

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 in 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 statement to include h2o library
3. Below is a quick start code example to get a kick start
4. checkout example and specs if you are in the mood for exploration.

*templates/index.html*

```

    Hello world

        Greetings {{ name }}

```

*index.php*

```

```

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

[](#useful-links)

- Please submit patches or bug report to our [lighthouse bug tracker](http://idealian.lighthouseapp.com/projects/11041-h2o-template-language)
- Checkout [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 attribute of a variable

#### variable lookup order

[](#variable-lookup-order)

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

**Example**

*in your template*

```
{{ person.name }}

```

*in php*

```

```

Let's say you have assigned a person variable in your php script, following variable tag will print out `Peter Jackson`

Filters
-------

[](#filters)

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

**Filter chaining**

[![filter chaining](https://camo.githubusercontent.com/6901a318a7dda47cb014f804d1224dfb24b7cafa57d65a4e0aeb46dbc671adf7/687474703a2f2f77696b692e73686f706966792e636f6d2f75706c6f61642f382f38632f46696c746572636861696e2e6a7067)](https://camo.githubusercontent.com/6901a318a7dda47cb014f804d1224dfb24b7cafa57d65a4e0aeb46dbc671adf7/687474703a2f2f77696b692e73686f706966792e636f6d2f75706c6f61642f382f38632f46696c746572636861696e2e6a7067)
Let me burrow 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 first 20 character of document description. Moreover, there are cases you want to pass multiple arguments and you can use comma( , ) to separate them `{{ person.bio|truncate 20, "..." }}`

**Filter named arguments**
h2o uses colon ( : ) to for named arguments to build optional arguments array.

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

and this translate to php will be this and that is pretty much what happen internally

```

```

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

Tag
---

[](#tag)

`{% tag %}`

Tags are usually very powerful, they controls the logical flow or structure, iteration. there are inline tags `{% inline_tag %}` or tags that requires a close tag. for example: `{% if condition %} ... {% endif %}`

### if tag

[](#if-tag)

if tag evaluate a variable or a simple expression. when results true the if tag body will be render or else part will be rendered.

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

```

### for tag

[](#for-tag)

For tag will iterate over a array of items.

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

```

Above will print all the tasks.

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

[](#template-inheritance)

H2o supports template inheritance, it is very powerful and the concept is easy to understand.

Template inheritance is implemented using block, extends tag, for programmers who is familiar with object oriented principles this is easy.

Quote from Django doc

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

*Example:*

*base.html* - to define the base structure of the page.

```

 {%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, sidebar) and HTML code common across all your page.

*page.html* - to define a template specific of a page.

```
{% 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, now you will be able to override any block previously defined.

There is a very good article about template inheritance in Django, in area of template inheritance h2o work exactly the same way.

[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 found you have a lot of common element inside the template, it may be a good idea to put that portion of template in side a block in a base template.
- block gives you a hook, especially useful they are useful for javascript, css too
- When defining a block use a short and distinctive name

### Configuration

[](#configuration)

There are a range of option to set up the template system the way you want it.

```

```

#### loader

[](#loader)

name of loader or a instance of H2o\_Loader

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

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

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

**Use dictionary loader**

You may want to load template from other resource than file then this will be your friend. h2o use `dict_loader()` for testing.

```
