-----------------
更新时间
18:13 2016-04-05 星期二
-----------------
* 概述
    QWeb是odoo主要模板引擎,采用xml表述,最后生成HTML文件
   
* 一般用法

#条件表达式
     <t t-if="record.effort_estimate.raw_value > 0">
        <li>Estimate <field name="effort_estimate"/></li>
     </t>
    
     比较符号:
       lt(<)    lte(<=)   gt(>)   gte(>=)
       注 < <= 不能用在表达式中,只能用字母代替
    
    # 输出值 t-esc 和 t-raw
       <t t-esc="record.message_follower_ids.raw_value" />
       <t t-raw="record.message_follower_ids.raw_value" />
       t-esc  过滤安全值,像html元素
       t-raw  数据库中的原始数据
   
    # 循环
        <t t-foreach="record.message_follower_ids.raw_value" t-as="rec">
           <t t-esc="rec" />;
        </t>
        t-foreach="record.message_follower_ids.raw_value.slice(0, 3)" 还可以切片
        还有一些变量
        rec_index 从0开始循环索引
        rec_size  要循环的记录集大小
        rec_first  第一个元素
        rec_last   最后一个元素
        rec_even   index为偶数时为真
        rec_odd    index为奇数时为真
        rec_parity  是偶数和奇数
        rec_all  表示循环结束的标识
        rec_value 循环一个字典时,的键的值
       
    # 动态属性
        <div>
            <t t-foreach="record.message_follower_ids.raw_value.slice(0, 3)"
            t-as="rec">
              <img t-att-src="kanban_image(
              'res.partner', 'image_small', rec)"
               class="oe_kanban_image oe_kanban_avatar_smallbox"/>
            </t>
        </div
        t-att- prefixed 如 <img>的src  就可以 t-att-src="..."
       
     # 属性的字符替换   
        <span t-attf-class="oe_kanban_text{{
            record.date_deadline.raw_value and
            !(record.date_deadline.raw_value > (new Date()))
            ? '_red' : '_black' }}">
            <field name="date_deadline"/>
        </span>
        t-attf-prefixed  取代内容,上面的就是动态类
       
    # 变量设置   
        #设置变量 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 t-name="follower_avatars">
            <div>
            <t t-foreach="record.message_follower_ids.raw_value.slice(0, 3)"
            t-as="rec">
                <img t-att-src="kanban_image(
                    'res.partner', 'image_small', rec)"
                    class="oe_kanban_image oe_kanban_avatar_smallbox"/>
            </t>
            </div>
        </t>   
        。。。
        <t t-call='follower_avatars' />
       
        t-call 调用其它模板
       
        复用灵活一些
        <t t-name="follower_avatars">
            <div>
            <t t-foreach="record.message_follower_ids.raw_value.slice(0, arg_max)"
            t-as="rec">
                <img t-att-src="kanban_image(
                    'res.partner', 'image_small', rec)"
                    class="oe_kanban_image oe_kanban_avatar_smallbox"/>
            </t>
            </div>
        </t>   
        。。。
        <t t-call='follower_avatars'>
           <t t-set="arg_max" t-value='3' />

<t/>

# QWeb 其它指令
       
        <p t-att="{'class': 'oe_bold', 'name': 'test1'}" />
        结果显示
        <p class="oe_bold" name="test1" />
        t-att 接受字典
        <p t-att="['class','oe_bold']"
        结果显示
        <p class="oe_bold">
       
    # card类式加菜单       
        <div class="oe_dropdown_kanban oe_dropdown_toggle">
            <span class="oe_e">í</span>
            <ul class="oe_dropdown_menu">
                <t t-if="widget.view.is_action_enabled('edit')">
                    <li>
                        <a type="edit">Edit...</a>
                    </li>
                </t>
                <t t-if="widget.view.is_action_enabled('delete')">
                    <li>
                        <a type="delete">Delete</a>
                    </li>
                </t>
                <!-- Color picker option: -->
                <li>
                    <ul class="oe_kanban_colorpicker"
                        data-field="color"/>
                </li>
            </ul>
        </div>
      
    # card类式加颜色    
        <field name="color" />   
        <div class="oe_kanban_card">
        <div t-attf-class="oe_kanban_card
              #{kanban_color(record.color.raw_value)}">
             
    # 为长文本加省略号
          <t t-esc="kanban_text_ellipsis(record.name.value, 32)" />
        过超32个字符就加... 不显示内容了
       
       
    # 自定义css    和javascript的资源
   
       <?xml version="1.0" encoding="utf-8"?>
        <openerp>
        <data>
            <template id="assets_backend"
                      inherit_id="web.assets_backend"
                      name="Todo Kanban Assets">
                <xpath expr="." position="inside">
                    <link rel="stylesheet"
                          href="/todo_kanban/static/src/css/todo_kanban.css"
                    />
                    <script type="text/javascript"
                            src="/todo_kanban/static/src/js/todo_kanban.js">
                    </script>
                </xpath>
            </template>
        </data>
        </openerp>
       
           
     # 调用其它模板
           采用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()方法
       
*代码分析   
     #扫描枪的操作界面
     <openerp>
       <data>
        <template id="assets_backend" name="stock assets" inherit_id="web.assets_backend">
            <xpath expr="." position="inside">
                <link rel="stylesheet" href="/stock/static/src/css/stock.css"/>
                <script type="text/javascript" src="/stock/static/src/js/widgets.js"></script>
            </xpath>
        </template>    
        .....
     </data>
    <openerp>

(21)odoo中的QWeb模板引擎的更多相关文章

  1. odoo中的QWeb模板引擎

    * 概述    QWeb是odoo主要模板引擎,采用xml表述,最后生成HTML文件    * 一般用法 #条件表达式 <t t-if="record.effort_estimate. ...

  2. 在express站点中使用ejs模板引擎

    在express站点中使用ejs模板引擎 文/玄魂 目录 在express站点中使用ejs模板引擎 前言 1.1         安装 1.2修改app.js 1.3创建测试页面 前言 使用 vs创建 ...

  3. .NET Core中使用Razor模板引擎

    一.简介 在MVC以外的场景中,我们往往需要完成一些模板引擎生成代码或页面的工作:在以前我们一般常用的有Razor.NVeocity.VTemplate.虽然所有的模板系统都具有一些共同特征,但 Ra ...

  4. 【转】在Express项目中使用Handlebars模板引擎

    原文:http://fraserxu.me/2013/09/12/Using-Handlebarsjs-with-Expressjs/ 最近在用Expressjs做一个项目,前后端都用它来完成.自己之 ...

  5. nodejs+Express中使用mustache模板引擎

    由于公司一个seo项目,需要我协助.此项目他人已经开发大半,由于seo需要,使用了服务器端模板引擎.我项目的后端同事说项目是go语音写的,跑项目麻烦,只给了我template和css等静态文件. 为了 ...

  6. NodeJS中使用swig模板引擎

    NodeJS中的默认引擎是jade有点过于复杂,而且不是以HTML为基础的,学习成本和前端适应成本都很大.而ejs虽然简单,但不支持模板导入,而且效率一般. swig的语法简单,学习成本很低,符合常规 ...

  7. ThinkPHP3.2.3中使用smarty模板引擎循环

  8. WebApi中利用Razor模板引擎来生成html

    在服务器端基于Razor来生成html的一个思路 using System.Web.Mvc; using System.IO; using System.Web.Routing; using Syst ...

  9. Odoo中Qweb使用入门

    参考 可参考官网例子https://doc.odoo.com/trunk/web/qweb/或 http://thierry-godin.developpez.com/openerp/tutorial ...

随机推荐

  1. IOS之分析网易新闻存储数据(CoreData的使用,增删改查)

    用过网易新闻客户端的朋友们都知道,获取新闻列表时有的时候他会请求网络有时候不会,查看某条新闻的时候再返回会标注已经查看的效果,接下来分析一下是如何实现的. 首先: 1.网易新闻用CoreData存储了 ...

  2. Mac功夫——OS X应用技巧

    看过不少文章说Mac是了不起的先进操作系统,爱折腾的我,经不住诱惑,也玩起了Mac,用惯了Windows,突然换到Mac下还真是十分不习惯,就连复制粘贴这种简单操作也觉得很是别扭. 用过一段时间才感觉 ...

  3. window删除损坏无法打开的文件

    移动硬盘删除文件时提示“文件或目录损坏且无法读取”的解决方法-chkdsk 命令的巧用 新买一个移动硬盘,同学借去Copy一个游戏,拷来后发现数据包损坏,提示"文件或目录损坏且无法读取&qu ...

  4. zoj 1010 (线段相交判断+多边形求面积)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Mem ...

  5. POJ 1142 Smith Numbers(史密斯数)

    Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...

  6. 生产者-消费者 用非阻塞队列、Object.wait()、Object.notify()实现

    非阻塞队列,需要考虑到: 1.并发中的同步 2.线程间通信 public class Quene_Pro_Con { //定义队列大小 private static int size = 10; // ...

  7. Redis数据导入工具优化过程总结

    Redis数据导入工具优化过程总结 背景 使用C++开发了一个Redis数据导入工具 从oracle中将所有表数据导入到redis中: 不是单纯的数据导入,每条oracle中的原有记录,需要经过业务逻 ...

  8. git学习笔记06-创建分支合并分支-比svn快多了,因为只有指针在改变

    一开始git只有一条时间线,这个分支叫主分支,即master分支. HEAD严格来说不是指向提交,而是指向master,master才是指向提交的,所以,HEAD指向的就是当前分支. 每次提交,mas ...

  9. Win7路由器设置过程

    随着应用win7系统的人越来越多,对于这个系统的应用就更多了,其中大家最关注的就是这个系统和路由器上网的问题.下面,我们就来讲解一下win7系统的路由器的设置过程. 首先打开浏览器,在地址栏输入192 ...

  10. netty概念

    Netty的ChannelFuture在Netty中的所有的I/O操作都是异步执行的,这就意味着任何一个I/O操作会立刻返回,不保证在调用结束的时候操作会执行完成.因此,会返回一个ChannelFut ...