oTree Forum >

Calling a variable value from previous app in the Template

#1 by Arsil

Hi Everyone,

I am working on an app with two apps where App1 is real effort task and App two is election game. In the template of Election game before voting, I define role of variables as candidate 1, candidate 2, and voter (3 person group) by using id_in_group as follows:

    def role2(player):
        if player.id_in_group == 3:
            return 'voter'
        elif player.id_in_group == 2:
            return 'candidate2'
            return 'candidate1'
I have checked that this role identification worked. However, when I called the variable of score in the previous app it gave me an error. I used the following code.

{{if group.get_player_by_id(1).participant.correctanswers_T1 > group.get_player_by_id(2).participant.correctanswers_T1 }}
    Candidate 1 has a higher score than Candidate 2 in Task 1.
 {{elif get_player_by_id(1).participant.correctanswers_T1 < get_player_by_id(2).participant.correctanswers_T1}}
    Candidate 1 has a lower score than Candidate 2 in Task 1.
 {{else }}
    Candidate 1 and Candidate 2 scores tied in Task 1.

I have also tried to use get_player_by_id(1).participant.correctanswers_T1 but it also did not work. Any help is much appreciated.

#2 by Chris_oTree

You can't put complex expressions like that in templates. Instead you should do it in vars_for_template:

def vars_for_template(player):
    p1_correct_T1 = group.get_player_by_id(1).participant.correctanswers_T1
    # etc...
    return dict(p1_correct_T1=p1_correct_T1)
Then in the template:

{{ if p1_correct_T1 > p2_correct_T1 }}        


#3 by Arsil

Hi Chris, Thank so much for the detailed reply. I have tried this code and it is working fine now as I expected.


Write a reply

Set forum username