<?xml version="1.0" encoding="UTF-8"?>
<wiki>
  <body>&lt;h1&gt;Filters&lt;/h1&gt;
&lt;h2&gt;Single filter&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;?php
require 'h2o/h2o.php';
$user = array('name'=&amp;gt;'peter', 'email'=&amp;gt; 'someone@gmail.com');
echo h2o('{{ user.email | avator | image_tag')-&amp;gt;render(compact('user'));
?&amp;gt;

h2o::addFilter('avator');
function avator($email) {
    return &quot;http://gravator.com/api?email=&quot;.md5($email);
}
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Filter collections&lt;/h2&gt;
&lt;p&gt;If you have a lot of related filters for your application, putting them into a filter collection class can give you better encapsulation and reuse on your methods.&lt;/p&gt;
&lt;p&gt;Here is some code example of a Filter collection class, h2o::addFilter() is a static method overloading a few different parameter options, if you pass-in a filter collection then all class methods will become h2o filters.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;?php
  h2o::addFilter('HtmlFilters');
  class HtmlFilters extends FilterCollection {
     function base_url($url) {
        return $url;
     }
     function script_tag($path) {
        return sprintf('&amp;lt;script type=&quot;text/javascript&quot; src=&quot;%s.js&quot;&amp;gt;', self::base_url($path));
     }
     ...
  }
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Tags&lt;/h1&gt;
&lt;p&gt;Tags are powerful stuff, creating a new tag allows you to&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;interact with h2o context and parser objects&lt;/li&gt;
	&lt;li&gt;implement new language construct, template macros&lt;/li&gt;
	&lt;li&gt;consuming external resources&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is favorite example of implementing a rss feed loader tag less than 20 lines with object caching&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;?php
require 'h2o/h2o.php'

h2o::addTag('load_feed_items');
class Load_feed_items_Tag extends H2o_Node {
    var $url, $cacheKey;
    
    function __construct($argstring, $parser, $pos=0) {
        $this-&amp;gt;url = trim($argstring);
        $this-&amp;gt;cacheKey = md5($this-&amp;gt;url);
    }

    function render($context, $stream) {
        $cache = h2o_cache($context-&amp;gt;options);

        # try cached 
        if (! ($feed = $cache-&amp;gt;read($this-&amp;gt;key))) {
            $feed = file_get_contents($this-&amp;gt;url);
            $cache-&amp;gt;write($this-&amp;gt;key, $feed);
        }
        $feed = simplexml_load_string($feed)-&amp;gt;xpath('//channel/item');
        $context-&amp;gt;set('feed_items', $feed);
    }
}

# Template part
$tpl = &amp;lt;&amp;lt;&amp;lt;TPL
    {% load_feed_items http://feeds.digg.com/digg/popular.rss %}
    {% for item in feed_items %}
        {{ item.title | links_to item.url }}
     {% endfor %}
TPL;

echo h2o($tpl)-&amp;gt;render();

&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;Extensions&lt;/h1&gt;</body>
  <created-at type="datetime">2010-02-09T19:11:52-08:00</created-at>
  <id type="integer">119751</id>
  <permalink>extending-h2o</permalink>
  <repository-id type="integer">11784</repository-id>
  <title>Extending H2o</title>
  <updated-at type="datetime">2009-04-07T03:31:40-07:00</updated-at>
  <user-id type="integer">70642</user-id>
</wiki>
