Page title
H2O template inheritance is a powerful tool
{% endblock %}H2O template markup ======================== Being a martial arts fan, I borrow a quote. 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 ------------------------ - PHP 5.1 + 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 h2o Download [](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 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. 3. Check out the *\example* and *\specs* dirs to see some of h2o's more interesting features in action. *templates/index.html*
H2O template inheritance is a powerful tool
{% endblock %}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][3] is a very good blog post explaining inheritance [3]:http://www2.jeffcroft.com/blog/2006/feb/25/django-templates-the-power-of-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 There are a range of options for configuring the template engine. [option_value] )); ?> #### 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 ); ?> __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. 'Hello {{ person }}' )); $template = new H2o('index.html', array('loader' => $loader')); ?> #### Searchpath default: this will be the base path of your template H2o use this path to load additional templates and extensions. You can either explicity set the search path, `$template = new H2o('index.html', array('searchpath' => '/sites/common_templates'));` or h2o will try to find the searchpath for you. `$template = new H2o('/sites/common_templates/index.html');` #### Cache You can define the type of caching engine h2o should use, if any. Set 'cache' to false to disable caching. You can read more about performance and caching in following sections Use file cache [default] `$template = new H2o('index.html', array('cache'=>'file'));` Use apc cache: `$template = new H2o('index.html', array('cache'=>'apc'));` Use memcache cache `$template = new H2o('index.html', array('cache'=>'memcache'));` Disable caching `$template = new H2o('index.html', array('cache'=>false));` #### Cache_dir When the file cache is used, you can define where you want templates to be cached. It will put a cached template in same location as the normal template `$template = new H2o('index.html', array('cache_dir'=>'/tmp/cached_templates'));` #### Cache_ttl "cache_ttl" specifies how long a cached template should be used (defaults: 1 hour) before it is recompiled. The template fragment cache that is bundled in the cache extension will use this as default ttl value. `$template = new H2o('index.html', array('cache_ttl' => 3600));` Performance and Caching ------------------------ Caching can increase performance since it skips step of inefficient template parsing. H2o caches the template objects (the internal data structure of a template) and the bundled caching backends include File, APC, and Memcache. ### File cache By default h2o uses the file cache to store template objects. Change h2o option `cache_dir` to where you want to store template cache (ie: /tmp). 'file', 'cache_dir' => '/tmp' )); ?> ### APC cache APC is an op-code and object cache extension for php whose performance is generally 10-30% better than just plain file caching. 'apc')); ?> ### Memcache Currently not implemented Extending H2o ------------------------ Known issues ------------------------ Yes, h2o has them. However, if you are careful, these shouldn't hinder your template development. The deep inheritance issue is a bit problematic for some template architectures, but again, if you are careful, and perhaps a bit inventive, it won't hinder you terribly much. * `{{ block.super }}` doesn't work with more than 1 level of inheritance yet, so if `{{ block.super }}` invokes another `{{ block.super }}` it won't work just yet. * 'if' conditions don't support multiple expressions or mathematical expressions yet, like: `{% if something > 3 or something < 2 %}` or `{% if something + else > 12 %}` These likely will not be implemented in the future unless some daring soul implements them and contributes the code back to the h2o-php project. Contributors --- - Taylor Luk - Founder of [Issue publishing](http://issueapp.com) - jlogsdon - Major refactoring (wip) and bug fixes - cyberklin - Added filter support for any context resolve - idlesign - Added if_changed tag support - metropolis - Improved our test coverage - plus many others Credit ------------------------ H2o borrows ideas and/or concepts from the following projects: - Django template - Django web development framework. - Ptemplates - Armin Ronacher's pet project for a django port in PHP. - Jinja - Django inspired template in Python. Special Thanks: Armin Ronacher, since early versions of h2o were based off of his Ptemplates project. The MIT License ------------------------ Copyright (c) 2008 Taylor Luk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.