Jinja2: Looking for a View-Helper
Question: I'am new to the Jinja2 template engine. Is there something like the view-
helpers from Zend Framework? Can i create simple functions and reuse them all
over all my template-files?

Something like this?

    
    
    #somewhere in my python code:
    def nice_demo_function(message):
        """"return a simple message"""
        return message
    

So i can to use that:

    
    
    <!-- now in my template-file -->
    {%  nice_demo_function('yes, this works great!') %}
    


Answer: There are a number of ways you can expose helper functions to your templates.
You could define them using
[macros](http://jinja.pocoo.org/2/documentation/templates#macros), and then
[import](http://jinja.pocoo.org/2/documentation/templates#import) them into
templates that use them. You could add functions to the [globals
attribute](http://jinja.pocoo.org/2/documentation/api#jinja2.Template.globals)
of your Template objects, or pass them to the
[render()](http://jinja.pocoo.org/2/documentation/api#jinja2.Template.render)
method. You could subclass Template to do the same without having to repeat
yourself each time. If you want to get really fancy, you could look into
writing [extensions](http://jinja.pocoo.org/2/documentation/extensions#jinja-
extensions) as well (but you probably don't need to go that deep).

