odoo中的QWeb模板引擎

(21)odoo中的QWeb模板引擎

* 概述
    QWeb是odoo主要模板引擎,采用xml表述,最后生成HTML文件
   
* 一般用法
    <t t-if="condition">
        <p>Test</p>
    </t>
    结果:
         <p>Test</p>
    可以看到采用t标签语句采用t-开头
    也可和hmtl标签混着用
    <div t-if="condition">
        <p>Test</p>
    </div>
     结果:
        <div>
          <p>Test</p>
        </div>
    ----------
     #数据输出 t-esc t-raw
         <p><t t-esc="value"/></p>
         t-esc 会转义html标签
         t-raw 原样输出内容
    
     ---------
     #条件语句 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>
         假设 $as 是t-as 的变量,
         <t t-foreach=list1 t-as=$as >
            #code
         </t>
         循环还有可用变量如下:
          $as_all:所有迭代的对象
          $as_value: 当前迭代的值
          $as_index: 当前迭代的索引值
          $as_size: 集合的大小
          $as_first:当前是否第一个值
          $as_last: 当前是否最后一个值
          $as_parity:'even'|'odd' 当前是第奇数还是偶数
          $as_even:当前是否循环到偶数次
          $as_odd:当前是否循环到奇数次
      ----------
     #设置变量 t-set t-value
        <t t-set="new_variable" t-value="True" />
        设置了变量 new_variable 的值 为 True
        t-value 也可以是表达
         <t t-set="foo" t-value="2+1" >
        设置了变量foo值为3
        t-value可以是一段html
        <t t-set="foo">
            <li>ok</li>
        </t>
        设置了变量foo 为 <li>ok</li>
       
     #设置属性
        t-att-$name
        $name 是属性名
        <div t-att-a="66" />
        结果:
          <div a="66"></div>
         
        t-attf-$name 用于混合字符串,变量用{{}}
        <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 键值对组成属性
        <div t-at="{'a':1,'b':2}" />
        结果:
         <div a="1" b="2"></div>
        
        t-att=pair 一对,这个对一个是键,后一个是值
         <div t-att="['a','b']" />  <=>    <div t-att="('a','b')" />   
        结果:
          <div a="b"></div>   
           
     # 调用其它模板
           采用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')  # 给用户的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">
                    <h1>Extended!</h1>
                </xpath>   
            </template>
             得到的结果:
               <h1>Extended!</h1>
               <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">
                    <h1>Extended!</h1>
                </xpath>   
            </template>   
              得到的结果:
               <p class="a">A</p>
               <h1>Extended!</h1>
               <p class="b">B</p>
            ----------
            调用系统的基础模板:
              <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">
                    <h1>Extended!</h1>
                </xpath>   
            </template>             
     #调试
       t-debug
        <t t-debug="pdb" />
        <=>
        importlib.import_module("pdb").set_trace()
       
     #python的请求模板
        response = http.request.render('my-template',{'context_value':99})
        用得是 http.request.render()方法             

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值