Skip to content

Using jinja2 as template engine for trytond report webkit

sharoonthomas edited this page Dec 17, 2014 · 5 revisions

We are big fans of jinja2 templating engine. If you are too, then there is no reason why you should be using Genshi and just use Jinja2 instead.

(If you think this should be part of the core, vote here: https://github.com/openlabs/trytond-report-webkit/issues/6) Jinja2 is the default since version 3.2

from openlabs_report_webkit import ReportWebkit

class JinjaWebkitHTMLReport(ReportWebkit):
    __name__ = 'report.jinja.sample'

    @classmethod
    def render_template(cls, template_string, localcontext, translator):
        """
        Render the template using Jinja2
        """
        env = Environment()
        env.filters.update({
            # Add whatever filters you need, the following come from babel.
            #'dateformat': partial(format_date, locale=Transaction().language),
            #'datetimeformat': partial(
            #    format_datetime, locale=Transaction().language
            #),
            #'currencyformat': partial(
            #    format_currency, locale=Transaction().language
            #),
        })
        report_template = env.from_string(template_string.decode('utf-8'))
        return report_template.render(**localcontext).encode('utf-8')
Clone this wiki locally