H2O templates not working with functions (method calls)

I love Django. And I love its template engine. It’s simply awesome, and template inheritance is such a powerful tool!

So, when I started developing in PHP with CodeIgniter, I hated its views and templates from the very beginning. Every second I dreamt of using Django views… And then I thought “Then I’ll code such an engine for PHP mysel!”

But, hey, are you mad? What are you thinking about? I don’t have time for that, and I don’t want to spend my time doing that, either. The first thing you should do is look up on the internet if there’s something like that!

And I found H2O (among others) and started using it. And happiness came to my life! …Until I realized I couldn’t evaluate method calls in the templates (although the syntax supports that).

So, if a class Field has a method widget_html(), and an instance variable field of class Field is visible from the template, this wouldn’t work:

{{ field.widget_html|safe }}

But why? If that’s supported by the syntax! After digging a bit on the internet (well, I didn’t have to dig that much; it was the second result), I found the solution on Stack Overflow. The issue is due to H2O needing to be told what methods are safe to be called from the templating language, and the way to do that is add an attribute to you class (in this case Field) where you specify the method names that are suitable for being called from the templates:

class Field {
    var $h2o_safe = array('label_html', 'widget_html');
    // ...

where we’re telling H2O that both methods label_html and widget_html of class Field are safe to be called.