qweb加html文件,QWeb — odoo 11.0 documentation

QWeb is the primary templating engine used by Odoo2. It

is an XML templating engine1 and used mostly to generate HTML

fragments and pages.

Template directives are specified as XML attributes prefixed with t-,

for instance t-if for conditionals, with elements

and other attributes being rendered directly.

To avoid element rendering, a placeholder element is also available,

which executes its directive but doesn’t generate any output in and of

itself:

Test

will result in:

Test

if condition is true, but:

Test

will result in:

Test

data output

QWeb has a primary output directive which automatically HTML-escape its

content limiting XSS risks when displaying user-provided content: esc.

esc takes an expression, evaluates it and prints the content:

rendered with the value value set to 42 yields:

42

There is one other output directive raw which behaves the same as

respectively esc but does not HTML-escape its output. It can be useful

to display separately constructed markup (e.g. from functions) or already

sanitized user-provided markup.conditionals

QWeb has a conditional directive if, which evaluates an expression given

as attribute value:

ok

The element is rendered if the condition is true:

ok

but if the condition is false it is removed from the result:

The conditional rendering applies to the bearer of the directive, which does

not have to be :

ok

will give the same results as the previous example.

Extra conditional branching directives t-elif and t-else are also

available:

Happy birthday!

Welcome master!

Welcome!

loops

QWeb has an iteration directive foreach which take an expression returning

the collection to iterate on, and a second parameter t-as providing the

name to use for the “current item” of the iteration:

will be rendered as:

1

2

3

Like conditions, foreach applies to the element bearing the directive’s

attribute, and

is equivalent to the previous example.

foreach can iterate on an array (the current item will be the current

value), a mapping (the current item will be the current key) or an integer

(equivalent to iterating on an array between 0 inclusive and the provided

integer exclusive).

In addition to the name passed via t-as, foreach provides a few other

variables for various data points:

Warning

$as will be replaced by the name passed to t-as$as_allthe object being iterated over$as_valuethe current iteration value, identical to $as for lists and integers,

but for mappings it provides the value (where $as provides the key)$as_indexthe current iteration index (the first item of the iteration has index 0)$as_sizethe size of the collection if it is available$as_firstwhether the current item is the first of the iteration (equivalent to

$as_index == 0)$as_lastwhether the current item is the last of the iteration (equivalent to

$as_index + 1 == $as_size), requires the iteratee’s size be

available$as_parityeither "even" or "odd", the parity of the current iteration round$as_evena boolean flag indicating that the current iteration round is on an even

index$as_odda boolean flag indicating that the current iteration round is on an odd

index

These extra variables provided and all new variables created into the

foreach are only available in the scope of the``foreach``. If the

variable exists outside the context of the foreach, the value is copied

at the end of the foreach into the global context.

attributes

QWeb can compute attributes on-the-fly and set the result of the computation

on the output node. This is done via the t-att (attribute) directive which

exists in 3 different forms:t-att-$namean attribute called $name is created, the attribute value is evaluated

and the result is set as the attribute’s value:

will be rendered as:

instead of just an expression, often useful to mix literal and non-literal

string (e.g. classes):

will be rendered as:

123t-att=mappingif the parameter is a mapping, each (key, value) pair generates a new

attribute and its value:

will be rendered as:

item of the pair is the name of the attribute and the second item is the

value:

will be rendered as:

QWeb allows creating variables from within the template, to memoize a

computation (to use it multiple times), give a piece of data a clearer name,

This is done via the set directive, which takes the name of the variable

to create. The value to set can be provided in two ways:a t-value attribute containing an expression, and the result of its

evaluation will be set:

will print 3

if there is no t-value attribute, the node’s body is rendered and set

as the variable’s value:

ok

will generate <li>ok</li> (the content is escaped as we

used the esc directive)

Note

using the result of this operation is a significant use-case for

the raw directive.calling sub-templates

QWeb templates can be used for top-level rendering, but they can also be used

from within another template (to avoid duplication or give names to parts of

templates) using the t-call directive:

This calls the named template with the execution context of the parent, if

other_template is defined as:

the call above will be rendered as

will be rendered as

1

.

However this has the problem of being visible from outside the t-call.

Alternatively, content set in the body of the call directive will be

evaluated before calling the sub-template, and can alter a local context:

The body of the call directive can be arbitrarily complex (not just

set directives), and its rendered form will be available within the called

template as a magical 0 variable:

This template was called with content:

being called thus:

content

will result in:

This template was called with content:

content

PythonExclusive directivesasset bundles“smart records” fields formatting

The t-field directive can only be used when performing field access

(a.b) on a “smart” record (result of the browse method). It is able

to automatically format based on field type, and is integrated in the

website’s rich text edition.

t-options can be used to customize fields, the most common option

is widget, other options are field- or widget-dependent.debuggingt-debuginvokes a debugger using PDB’s set_trace API. The parameter should

be the name of a module, on which a set_trace method is called:

is equivalent to importlib.import_module("pdb").set_trace()HelpersRequest-based

Most Python-side uses of QWeb are in controllers (and during HTTP requests),

in which case templates stored in the database (as

views) can be trivially rendered by calling

odoo.http.HttpRequest.render():

response = http.request.render('my-template', {

'context_value': 42

})

This automatically creates a Response object which can

be returned from the controller (or further customized to suit).View-based

At a deeper level than the previous helper is the render method on

ir.ui.view:render(cr, uid, id[, values][, engine='ir.qweb][, context])

Renders a QWeb view/template by database id or external id.

Templates are automatically loaded from ir.ui.view records.

Sets up a number of default values in the rendering context:requestthe current WebRequest object, if anydebugwhether the current request (if any) is in debug modeurl-encoding utility functionthe corresponding standard library modulethe corresponding standard library modulethe corresponding standard library modulesee modulekeep_querythe keep_query helper function

Parameters

values – context values to pass to QWeb for rendering

engine (str) – name of the Odoo model to use for rendering, can be

used to expand or customize QWeb locally (by creating

a “new” qweb based on ir.qweb with alterations)JavascriptExclusive directivesdefining templates

The t-name directive can only be placed at the top-level of a template

file (direct children to the document root):

It takes no other parameter, but can be used with a element or any

other. With a element, the should have a single child.

The template name is an arbitrary string, although when multiple templates

are related (e.g. called sub-templates) it is customary to use dot-separated

names to indicate hierarchical relationships.template inheritance

Template inheritance is used to alter existing templates in-place, e.g. to

add information to templates created by other modules.

Template inheritance is performed via the t-extend directive which takes

the name of the template to alter as parameter.

When t-extend is combined with t-name a new template with the given name

is created. In this case the extended template is not altered, instead the

directives define how to create the new template.

In both cases the alteration is then performed with any number of t-jquery

sub-directives:

new element

The t-jquery directives takes a CSS selector. This selector is used

on the extended template to select context nodes to which the specified

t-operation is applied:appendthe node’s body is appended at the end of the context node (after the

context node’s last child)prependthe node’s body is prepended to the context node (inserted before the

context node’s first child)beforethe node’s body is inserted right before the context nodeafterthe node’s body is inserted right after the context nodeinnerthe node’s body replaces the context node’s childrenreplacethe node’s body is used to replace the context node itselfattributesthe nodes’s body should be any number of attribute elements,

each with a name attribute and some textual content, the named

attribute of the context node will be set to the specified value

(either replaced if it already existed or added if not)No operationif no t-operation is specified, the template body is interpreted as

javascript code and executed with the context node as this

Warning

while much more powerful than other operations, this mode is

also much harder to debug and maintain, it is recommended to

avoid itdebugging

The javascript QWeb implementation provides a few debugging hooks:t-logtakes an expression parameter, evaluates the expression during rendering

and logs its result with console.log:

will print 42 to the consolet-debugtriggers a debugger breakpoint during template rendering:

will stop execution if debugging is active (exact condition depend on the

browser and its development tools)t-jsthe node’s body is javascript code executed during template rendering.

Takes a context parameter, which is the name under which the rendering

context will be available in the t-js’s body:

console.log("Foo is", ctx.foo);

Helperscore.qweb

(core is the web.core module) An instance of QWeb2.Engine() with all module-defined template

files loaded, and references to standard helper objects _

(underscore), _t (translation function) and JSON.

core.qweb.render can be used to

easily render basic module templatesAPIclassQWeb2.Engine()

The QWeb “renderer”, handles most of QWeb’s logic (loading,

parsing, compiling and rendering templates).

OpenERP Web instantiates one for the user in the core module, and

exports it to core.qweb. It also loads all the template files

of the various modules into that QWeb instance.

A QWeb2.Engine() also serves as a “template namespace”.QWeb2.Engine.QWeb2.Engine.render(template[, context])

Renders a previously loaded template to a String, using

context (if provided) to find the variables accessed

during template rendering (e.g. strings to display).

Arguments

template (String) – the name of the template to render

context (Object) – the basic namespace to use for template

rendering

Returns

String

The engine exposes an other method which may be useful in some

cases (e.g. if you need a separate template namespace with, in

OpenERP Web, Kanban views get their own QWeb2.Engine()

instance so their templates don’t collide with more general

“module” templates):QWeb2.Engine.QWeb2.Engine.add_template(templates)

Loads a template file (a collection of templates) in the QWeb

instance. The templates can be specified as:An XML stringQWeb will attempt to parse it to an XML document then load

it.A URLQWeb will attempt to download the URL content, then load

the resulting XML string.A Document or NodeQWeb will traverse the first level of the document (the

child nodes of the provided root) and load any named

template or template override.

A QWeb2.Engine() also exposes various attributes for

behavior customization:QWeb2.Engine.QWeb2.Engine.prefix

Prefix used to recognize directives during parsing. A string. By

default, t.QWeb2.Engine.QWeb2.Engine.debug

Boolean flag putting the engine in “debug mode”. Normally,

QWeb intercepts any error raised during template execution. In

debug mode, it leaves all exceptions go through without

intercepting them.QWeb2.Engine.QWeb2.Engine.jQuery

The jQuery instance used during template inheritance processing.

Defaults to window.jQuery.QWeb2.Engine.QWeb2.Engine.preprocess_node

A Function. If present, called before compiling each DOM

node to template code. In OpenERP Web, this is used to

automatically translate text content and some attributes in

templates. Defaults to null.

Genshi, although it does not use (and

has no support for) XML namespaces

Jinja and Mako.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值