.. _templates: Templates ========= Template syntax --------------- Variables ~~~~~~~~~ You can display a variable like this: .. code-block:: django Your payoff is {{ player.payoff }}. The following variables are available in templates: - ``player``: the player currently viewing the page - ``group``: the group the current player belongs to - ``subsession``: the subsession the current player belongs to - ``participant``: the participant the current player belongs to - ``session``: the current session - ``C`` - Any variables you passed with :ref:`vars_for_template`. Conditions ("if") ~~~~~~~~~~~~~~~~~ .. note:: oTree 3.x used two types of tags in templates: ``{{ }}`` and ``{% %}``. Starting in oTree 5, however, you can forget about ``{% %}`` and just use ``{{ }}`` everywhere if you want. The old format still works. .. code-block:: {{ if player.is_winner }} you won! {{ endif }} With an 'else': .. code-block:: {{ if some_number >= 0 }} positive {{ else }} negative {{ endif }} Complex example: .. code-block:: {{ if a or b }} ..... {{ elif c and d }} ..... {{ else }} ..... {{ endif }} Loops ("for") ~~~~~~~~~~~~~ .. code-block:: django {{ for item in some_list }} {{ item }} {{ endfor }} Accessing items in a dict ~~~~~~~~~~~~~~~~~~~~~~~~~ Whereas in Python code you do ``my_dict['foo']``, in a template you would do ``{{ my_dict.foo }}``. Comments ~~~~~~~~ .. code-block:: django {# this is a comment #} {# this is a multiline comment #} Things you can't do ~~~~~~~~~~~~~~~~~~~ The template language is just for displaying values. You can't do math (``+``, ``*``, ``/``, ``-``) or otherwise modify numbers, lists, strings, etc. For that, you should use :ref:`vars_for_template`. How templates work: an example ------------------------------ oTree templates are a mix of 2 languages: - *HTML* (which uses angle brackets like ```` and ````). - *Template tags* (which use curly braces like ``{{ this }}``) In this example, let's say your template looks like this: .. code-block:: html

Your payoff this round was {{ player.payoff }}.

{{ if subsession.round_number > 1 }}

Your payoff in the previous round was {{ last_round_payoff }}.

{{ endif }} {{ next_button }} Step 1: oTree scans template tags, produces HTML (a.k.a. "server side") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ oTree uses the current values of the variables to convert the above template tags to plain HTML, like this: .. code-block:: html

Your payoff this round was $10.

Your payoff in the previous round was $5.

Step 2: Browser scans HTML tags, produces a webpage (a.k.a. "client side") ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The oTree server then sends this HTML to the user's computer, where their web browser can read the code and display it as a formatted web page: .. figure:: _static/template-example.png Note that the browser never sees the template tags. The key point ~~~~~~~~~~~~~ If one of your pages doesn't look the way you want, you can isolate which of the above steps went wrong. In your browser, right-click and "view source". (Note: "view source" may not work in split-screen mode.) You can then see the pure HTML that was generated (along with any JavaScript or CSS). - If the HTML code doesn't look the way you expect, then something went wrong on the server side. Look for mistakes in your ``vars_for_template`` or your template tags. - If there was no error in generating the HTML code, then it is probably an issue with how you are using HTML (or JavaScript) syntax. Try pasting the problematic part of the HTML back into a template, without the template tags, and edit it until it produces the right output. Then put the template tags back in, to make it dynamic again. Images (static files) --------------------- The simplest way to include images, video, 3rd party JS/CSS libraries, and other static files in your project is to host them online, for example on Dropbox, Imgur, YouTube, etc. Then, put its URL in an or