2015-09-10 79 views
3

我试图使用相同的功能在Django如何使用:如果Odoo模板语言

<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> 

但寻找一个更Python的方式

回答

5

我不认为QWeb模板甚至意图是Python的;)

你可以这样做如果你想:

<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> 
+0

Ludwik Trammer the odoo guru! –

+0

好工作的人!!!! –

+0

有没有什么办法可以在那里使用elif?我的意思是 - elif在attf ... –

1

尝试以下,

在小部件中准备一个函数来比较value和设置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"/> 
+0

我不认为内联样式是(永远)要走的路... –

+0

这是一个替代方法来做一些棘手的东西! –