【问题标题】:How to use if in Odoo template language如何在 Odoo 模板语言中使用 if
【发布时间】:2015-09-10 03:20:35
【问题描述】:

我正在尝试使用与 Django 中相同的功能:

<div class="item {% if condition == True %}active{% endif %}">

在 Odoo 我有:

<t t-foreach="list" t-as="l">
    <a t-attf-href="/downloads/{{l.id}}" class="list-group-item"><t t-esc="l.name"/></a>
</t>

如果“c.id = cat_id”,我需要附加类“活动”

如何在 Odoo 中完成?

我正在使用:

<t t-foreach="categories" t-as="c">
    <t t-if="c.id == category_id">
    <a t-attf-href="/downloads/{{c.id}}" class="list-group-item active"><t t-esc="c.name"/></a>
    </t>
    <t t-if="c.id != category_id">
    <a t-attf-href="/downloads/{{c.id}}" class="list-group-item"><t t-esc="c.name"/></a>
    </t>
</t>

但是寻找一种更pythonic的方式

【问题讨论】:

    标签: odoo odoo-8 odoo-website


    【解决方案1】:

    我认为 QWeb 模板甚至不打算成为 Pythonic ;)

    如果你愿意,你可以这样做:

    <a t-attf-href="/downloads/{{c.id}}" t-attf-class="list-group-item {{ 'active' if c.id == category_id else '' }}">
        <t t-esc="c.name"/>
    </a>
    

    【讨论】:

    • 卢德维克·特拉默 odoo 大师!
    【解决方案2】:

    尝试关注,

    在小部件中准备一个函数来比较值并将 css 样式设置为该小部件的新属性。

    init: function(parent,options){
            //define 1 property to access this in template and set it from calling function
            this.style = '';
            this._super(parent,options);
        }
        get_comparison_result : function(cid,category_id){
            var style = "";
            if (cid != category_id){
                //Add style here
                style = "background-color:white";
            }
            else{
                //Add style here
                style = "background-color:green";
            }
            this.style = style;
            return true;
        }
    

    然后您可以从模板中调用它并将结果分配给变量并使用此结果。

    <t t-if="widget.get_comparison_result(c.id, category_id)">
        <t t-set="style" t-value="widget.style" />
    </t>
    
    <a t-attf-href="/downloads/{{c.id}}" t-att-style="style"><t t-esc="c.name"/>
    

    【讨论】:

      猜你喜欢
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 2017-03-25
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多