#1 by PatrickT
Hi all, I was attempting to reference a player level variable in Javascript. I am using Otree Studio and I attempted to do this with: firm_id={{Player.id_in_group |json}} I saw a similar line of code in a video so I thought this was the proper way to do such but when testing my experiment I got the error "UndefinedVariable: Cannot resolve the variable 'Player' (line 79, in "Player.id_in_group")" Thank you all for the help! Best, Patrick T
#2 by PatrickT
I think I figured out that the fix might be firm_id={{player.id_in_group |json}}. The video specifically had the P capitalized though. I suppose it could have been a typo.
#3 by BonnEconLab
Just firm_id = {{ player.id_in_group }} (that is, without the “| json” part) also works when I test it (using oTree v5.10.3). For more complex stuff, you can use the js_vars(player) method provided by oTree, see https://otree.readthedocs.io/en/latest/templates.html#passing-data-from-python-to-javascript-js-vars. For instance, in the __init__.py, include @staticmethod def js_vars(player): return dict( firm_id = "Your Firm ID is" + str(player.id_in_group) + ".", ) You can then address the result as js_vars.firm_id in your JavaScript code.
#4 by PatrickT
Thank you for the insight! I was wondering whether the "| json" was necessary.