Page rendered in 0.05904s using 11 queries. *index.php* render(array('name'=>'Peter Jackson')); ?> Useful links ------------------------ * Please submit patches or bug reports to our [lighthouse bug tracker][issue]. * Check out our [Google group][group] for h2o-related discussion. [issue]:http://idealian.lighthouseapp.com/projects/11041-h2o-template-language [group]:http://groups.google.com/group/h2o-template-php Syntax explanation ------------------------ ## variable `{{ variable }}` Use dot (.) to access attributes of a variable #### 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* 'Peter Jackson', 'age' => 25 ); $h2o->render(compact('person')); ?> 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 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](http://wiki.shopify.com/upload/8/8c/Filterchain.jpg) 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: 450, 'height' => 250, 'alt'=>'company logo' )); ?> 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 %}` 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 `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 `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 ------------------------ 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.
H2O template inheritance is a powerful tool
{% endblock %}