oTree Forum >

Display player.field input based on role or ID_in_group

#1 by caroooo

Hello,

in our experiment we have 6 players per group that we divide into subgroups, so that roles A, B, C (or ID_in_group 1, 2, 3) are subgroup 1 and roles D, E, F (or ID_in_group 4, 5, 6) are subgroup 2. Everything works fine except for one part.

All 6 players enter a number in a "proposal" field that needs to be displayed on the next page which isn't the results page.

We haven't found a solution for how these inputs can only be shown for the specific subgroup and not for all players in the 6-player-group, as the following code snippet does:


{% for p in group.get_players %}
{{ p.role }}:
{{ p.proposal}}
{% endfor %}

How can this code be altered so that it only shows p.proposal for role A, B, C and seperately for roles D, E, F?

Any help or advice is highly appreciated!

Greetings,
Caro

#2 by caroooo

For anyone else who is still learning, this is how I solved it:

In init.py:

@staticmethod
def vars_for_template(player):
players = player.group.get_players()
subgroup1 = [p for p in players if p.id_in_group <= 3]
subgroup2 = [p for p in players if p.id_in_group > 3]
return dict(
    subgroup1=subgroup1,
    subgroup2=subgroup2,
)

On the page:

{% if player.id_in_group <= 3 %}
{% for p in subgroup1  %}
<table>
<tr>
<td>{{ p.role }}:</td>
<td>{{ p.proposal}}</td>
</tr>
</table>
{{endfor}}
<br>
{{else}}
{% for p in subgroup2  %}
<table>
<tr>
<td>{{ p.role }}:</td>
<td>{{ p.proposal}}</td>
</tr>
</table>
{{endfor}}
<br>

Write a reply

Set forum username