3

just wondering what the deal is with the scope of variables in a tornado template. I have this code in a template, but it fails!

    {% set dictionary = {'a' : 1, ... more letters here ... 'z' : 26} %}
    {% set sum = 0 %}
    {% for y in x %}
        {% sum += int(dictionary[y.lower()]) #x is a string, y a char %}
    {% end %}
    {{ sum }}

But I get:

ParseError: unknown operator: 'sum'

What's going on?

4

1 回答 1

7

只需set在之前使用sum +=

{% set dictionary = {'a' : 1, ... more letters here ... 'z' : 26} %}
{% set sum = 0 %}
{% for y in x %}
    {% set sum += int(dictionary[y.lower()]) #x is a string, y a char %}
{% end %}
{{ sum }}
于 2012-08-15T02:29:05.343 回答