oTree Forum >

TemplateSyntaxError: Unrecognised filter name 'stringformat:"s"'

#1 by manond

Hey all,

I am copy/pasting a message from ggle forum as I have exactly the same issue. Could someone help me on this issue? Many thanks !!

I am trying to replicate Felix Holzmeister's MPL game from
https://github.com/felixholzmeister/mpl

The error I get when running it in oTree 5 is the following: 
Unrecognised filter name 'stringformat:"s"'


The full code for that portion is below.  


{% for i,f,p in choices %}
<tr>
<td class="pie-chart">
{% with j=i|stringformat:"s" %}
{% with pies_a="pie_a_"|add:j %}
<div id="{{ pies_a }}" class="pie-chart"></div>
{% endwith %}
{% endwith %}
</td>
<td class="pie-chart">
{% with j=i|stringformat:"s" %}
{% with pies_b="pie_b_"|add:j %}
<div id="{{ pies_b }}" class="pie-chart"></div>
{% endwith %}
{% endwith %}
</td>
</tr>
<tr>
<td class="center">
<button name="{{ f }}" value="A" class="btn btn-primary">
{% trans "Option A" %}
</button>
</td>
<td class="center">
<button name="{{ f }}" value="B" class="btn btn-primary">
{% trans "Option B" %}
</button>
</td>
</tr>
{% endfor %}

#2 by BonnEconLab

The code does not work in oTree 5 because the |stringformat filter and |add filter are provided by Django, and oTree 5 is not based on Django anymore.

This forces you to think more closely in which way you would information defined in the .py files to be used in the HTML templates. In other words, you have to adapt the __init__.py such that it provides the strings etc. in the final format that you would like to use in the template. That is, you will have to do the string formatting and concatenation in the .py file.

You can still access nested lists and dictionaries via loops. Say, you define

    @staticmethod
    def vars_for_template(player):
        #player.subsession.session.countCondition = player.subsession.session.countCondition + 1
        # BEWARE! This will increment the counter whenever a participant refreshes the page.
        list = [[11, 12, 13], [21, 22, 23]]
        dict = {"a": 1, "b": 2, "c": 3}
        return {
            "test": player.session.countCondition,
            "test_list": list,
            'test_dict': dict,
        }

in the __init__.py. You can then include

    {% for l in test_list %}
        {% for e in l %}
            {{ e }}
        {% endfor %}
    {% endfor %}

    {% for k, e in test_dict.items %}
        <br>{{ k }}: {{ e }}
        <br>class="entry_{{ k }}{{ e }}"
    {% endfor %}

in the HTML template.

#3 by manond

Thanks a lot !

Write a reply

Set forum username