* Overview
Qweb is the Odoo main template engine, using XML representation, and finally generating HTML files
* General Usage
<t t-if= "Condition" >
<p>Test</p>
</t>
Results:
<p>Test</p>
You can see that the T-tag statement takes the tee-start
can also be mixed with the HMTL label
<div t-if= "Condition" >
<p>Test</p>
</div>
Results:
<div>
<p>Test</p>
</div>
----------
#数据输出 T-esc T-raw
<p><t t-esc= "Value"/></p>
T-esc will escape HTML tags
T-raw output content as-is
---------
#条件语句 t-if
<div>
<p t-if= "Condition" >ok</p>
</div>
---------
#循环语句 T-foreach T-as
<t t-foreach= "[1, 2, 3]" t-as= "I" >
<p><t t-esc= "I"/></p>
</t>
Suppose $as is a variable of t-as,
<t t-foreach=list1 t-as= $as >
#code
</t>
The loop also has the following available variables:
$as _all: Objects for all iterations
$as _value: The value of the current iteration
$as _index: The index value of the current iteration
$as _size: The size of the collection
$as _first: Whether the first value is currently
$as _last: Whether the last value is currently
$as _parity: ' Even ' | ' Odd ' is currently the odd or even number
$as _even: is currently looping to even several times
$as _odd: Whether the current loop to odd number of times
----------
#设置变量 T-set T-value
<t t-set= "new_variable" t-value= "True"/>
Set the value of the variable new_variable to True
T-value can also be expressed
<t t-set= "foo" t-value= "2+1" >
Set the variable foo value to 3
T-value can be a section of HTML
<t t-set= "foo" >
<li>ok</li>
</t>
Set the variable foo to <li>ok</li>
#设置属性
t-att-$name
$name is the property name
<div t-att-a= "/>"
Results:
<div a= "></div>"
t-attf-$name used to mix strings, variables with {{}}
<t t-foreach= "[1, 2, 3]" t-as= "Item" >
<li t-attf-class= "row {{item_parity}}" ><t t-esc= "item"/></li>
</t>
T-att=mapping Key-value pairs constituent attributes
<div t-at= "{' A ': 1, ' B ': 2}"/>
Results:
<div a= "1" b= "2" ></div>
T-att=pair pair, this one is the key, the latter one is the value
<div t-att= "[' A ', ' B ']"/> <=> <div t-att= "(' A ', ' B ')"/>
Results:
<div a= "B" ></div>
# Call Other templates
With T-call
<template id= "Sub" >
<t t-esc= "identifier"/>
</template>
<template id= "Hello" >
<p>
Hello
<t t-call= "Module.sub" >
<t t-set= "identifier" t-value= "name"/>
</t>
</p>
</template>
#字段渲染
@http. Route (' Hello/<model (' res.users '): User ') # give your users an ID
def hello (self,user,**kw)
Return Http.request.render (' Module.hello ', {' user ': User})
-------
<template id= "Hello" >
<p t-field= "User.display_name"/>
</template>
---------
#可用字段选择修饰
<template id= "Hello" >
<p t-field= "User.creat_date"/>
<p t-field= "User.creat_date" t-filed-options= ' {"format": "Long"} '/>
<p t-field= "User.creat_date" t-filed-options= ' {"format": "EEE"} '/>
</template>
-------------
<template id= "Hello" >
<p t-field= "User.wealth"/>
<p t-field= "User.wealth" t-filed-options= ' {
"Widget": "Monetary"
"Display_currency": "user.company_id.currency_id"
} '/>
</template>
------------
<template id= "Hello" >
<p t-field= "User.create_date" t-field-options= ' {"widget": relative} '/>
</template>
#模板继承
<template id= "Hello" >
<p> Base Template </p>
</template>
<template id= "Hello2" inherit_id= "Hello" name= "Extender" >
<xpath expr= "//p" position= "before" >
</xpath>
</template>
The results obtained:
<p>base template</p>
--------------
<template id= "Hello" >
<p class= "a" >A</p>
<p class= "B" >B</p>
</template>
<template id= "Hello2" inherit_id= "Hello" name= "Extender" >
<xpath expr= "//p[hasclass (' B ')]" position= "before" >
</xpath>
</template>
The results obtained:
<p class= "a" >A</p>
<p class= "B" >B</p>
----------
To invoke the system's underlying template:
<template id= "Hello" >
<t t-call= "Website.layout" >
<p class= "a" >A</p>
<p class= "B" >B</p>
</t>
</template>
<template id= "Hello2" inherit_id= "Hello" name= "Extender" >
<xpath expr= "//p[hasclass (' B ')]" position= "before" >
</xpath>
</template>
#调试
T-debug
<t t-debug= "pdb"/>
<=>
Importlib.import_module ("PDB"). Set_trace ()
#python的请求模板
Response = Http.request.render (' my-template ', {' Context_value ': 99})
The Http.request.render () method is used.
(+) Qweb template engine in Odoo