D
D
damir_in2013-08-24 07:37:07
Kohana
damir_in, 2013-08-24 07:37:07

How to call a helper in a Twig template?

Hello!

I am using twig as a templating engine. Ordinary methods can be called object.method, but this does not work for static methods. Who faced such a task?

The module itself is github.com/tommcdo/kohana-twig

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
damir_in, 2013-08-24
@damir_in

I decided! While with hardcode. Create class in folder /application/classes/Twig.php:

<?php defined('SYSPATH') or die('No direct script access.');

class Twig extends Kohana_Twig {

    protected static function env()
    {
        // Instantiate the base Twig environment from parent class.
        $env = parent::env();

        // Customize as needed.
        $env->addFunction('base_url', new Twig_Function_Function('URL::base'));

        return $env;
    }

} // End Twig

We call the helper in the template:
{% block content %}
   {{ base_url() }}
{% endblock %}

The result will be -/kohana/

R
return, 2013-08-24
@return

Twig is very customizable, you can write your own extension, in which you can add your own functions, filters, post-processors, and whatever.
Even the basic functions of Twig are highlighted in it in the Core extension, you can see how it is done. And read the documentation, it is very short and understandable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question