Skip to Content
Menu
This question has been flagged
2 Replies
1482 Views

Here is the template code where it doesn't work as expected. I expected [10, 10, 10] for foo array but insted it shows [0, 1, 2] when I display the output. 


< t t-set = "foo" t-value = "[]" />

< t t-foreach = "[1,2,3]" t-as = "i" > 
< t t-set = "foo" t-value = "foo + [10]" />
< p > < t t-esc = "i" /> </ p >
</ t >

the below piece of code also throws an error of No append function.
< t t-set = "foo" t-value = "[]" />

< t t-set = "foo" t-value = "foo.append(10)"/>
Avatar
Discard
Best Answer

In Odoo templates, the t-set attribute is used to set a variable to a given value. However, t-set doesn't support modifying a list in-place, such as appending elements to it.

To accomplish this, you can use the t-operation attribute instead, which allows you to perform operations on the value of a variable. In your case, you can use the t-operation attribute to append elements to the foo list as follows:

t-set="foo" t-value="[]"/>

t-foreach="[1,2,3]" t-as="i"> 
  t-operation="foo.append(10)"/>
  

t-esc="i"/>

Alternatively, you can use the t-call attribute to call a Python function that modifies the list in-place. Here is an example:

!-- Define a Python function that appends 10 to the given list -->
t-call="website.append_to_list" t-args="foo"/>


t-foreach="[1,2,3]" t-as="i">
  t-call="website.append_to_list" t-args="foo"/>
  

t-esc="i"/>

foo = t-esc="foo"/>

In the Python code of your website, you would need to define the append_to_list function as follows:

def append_to_list(foo):
  foo.append(10)

Hope this helps!

Avatar
Discard
Best Answer

Hi,

Please try like as follows:-

<t t-set="foo" t-value="[]"/>


<t t-foreach="[1,2,3]" t-as="i">


    <t t-set="foo" t-value="foo + [10]"/>


</t>


<p>


    <t t-esc="foo"/>


</p>


Regards

Avatar
Discard
Related Posts Replies Views Activity
0
Oct 24
3076
1
Mar 24
940
1
Apr 22
1861
1
Mar 22
1230
0
Jun 21
2292