Accessing an array defined in vars_for_template

491 views
Skip to first unread message

Elif Bike Ösün

unread,
Feb 15, 2022, 4:29:26 PM2/15/22
to oTree help & discussion
Hi!

I am defining some variables in vars_for_template, including an array r that contains 10 elements. When I try to call the values in the page in the block conent:

  <td><span style="color: rgb(95, 95, 95)"><math>{{ r[0] }}&percnt;</math></span></td>

I get the following error:
Application error (500)TemplateSyntaxError: Unparsable expression 'r[0]' (line 200, in "r[0]")

I can call other variables just fine, but it seems like the array items are the issue. What is the correct way to call an element from an array defined in vars_for_template?

Best,
Elif

Chris @ oTree

unread,
Feb 15, 2022, 7:38:08 PM2/15/22
to Elif Bike Ösün, oTree help & discussion
r.0

Sent from my phone

On Feb 16, 2022, at 5:29 AM, Elif Bike Ösün <elifbi...@gmail.com> wrote:

Hi!
--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/b1ff9aa0-0692-4a48-8b8a-29f41aa987bfn%40googlegroups.com.

Elif Bike Ösün

unread,
Feb 15, 2022, 8:06:42 PM2/15/22
to oTree help & discussion
Wonderful, works now. Thank you Chris.

Elif Bike Ösün

unread,
Feb 16, 2022, 11:18:10 AM2/16/22
to oTree help & discussion
Sorry for the consecutive thread, but I have follow up question(s),

I now get an error when I try to access this array in a for loop: 

{{ for i in range(10, 0, -1) }}
{{ if player.round_number >= i }}
<tr>
<td><span style="color: rgb(95, 95, 95)">{{ i }}</span></td>
<td>&emsp;</td>
<td><span style="color: rgb(95, 95, 95)">{{ r.i }}&percnt;</span></td>
<td>&emsp;</td>
<td><span style="color: rgb(95, 95, 95)">{{ p.i }}</span></td>
</tr>
{{ endif }}
{{ endfor }}

Results in the following error: 
Application error (500)UndefinedVariable: Cannot resolve the variable 'r.i' (line 129, in "r.i") 

Is it not possible to access the array in a for loop?

On a related issue, 10 is the number of rounds in the experiment. If I try to replace the 10 in the expression "range(10, 0, -1)" with Constants.num_rounds, I get the following error:
TemplateSyntaxError: Unparsable argument 'Constants.num_rounds'. Arguments must be valid Python literals. (line 124, in "for")

Chris @ oTree

unread,
Feb 16, 2022, 11:39:49 AM2/16/22
to oTree help & discussion
That is not possible to do in templates. You can use numbers like r.0, r.1, etc., but not r.i where i is a variable.
This would need to all be computed in vars_for_template.

Chris @ oTree

unread,
Feb 16, 2022, 11:41:12 AM2/16/22
to oTree help & discussion
In vars_for_template you can either build a list with the values for each row, or you could even build the whole table HTML in python code.
Reply all
Reply to author
Forward
0 new messages