🦉 QWeb Engine 🦉

Content

Overview

QWeb is the primary templating engine used by Odoo. The QWeb class in the OWL project is an implementation of that specification with a few interesting points:

We present in this section the engine, not the templating language.

Reference

This section is about the javascript code that implements the QWeb specification. Owl exports a QWeb class in owl.QWeb. To use it, it just needs to be instantiated:

const qweb = new owl.QWeb();

Its API is quite simple:

In some way, a QWeb instance is the core of an Owl application. It is the only mandatory element of an environment. As such, it has an extra responsibility: it can act as an event bus for internal communication between Owl classes. This is the reason why QWeb actually extends EventBus.

Translations

If properly setup, Owl QWeb engine can translate all rendered templates. To do so, it needs a translate function, which takes a string and returns a string.

For example:

const translations = { hello: "bonjour", yes: "oui", no: "non", }; const translateFn = (str) => translations[str] || str; const qweb = new QWeb({ translateFn });

Once setup, all rendered templates will be translated using translateFn:

So, with the above translateFn, the following templates:

<div>hello</div> <div t-translation="off">hello</div> <div>Are you sure?</div> <input placeholder="hello" other="yes"/>

will be rendered as:

<div>bonjour</div> <div>hello</div> <div>Are you sure?</div> <input placeholder="bonjour" other="yes"/>

Note that the translation is done during the compilation of the template, not when it is rendered.