2012-08-15 81 views
2

只是想知道龙卷风模板中变量的作用范围。我在模板中有这个代码,但它失败了!龙卷风局部变量范围(python)

{% 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 }} 

,但我得到:

ParseError: unknown operator: 'sum' 

这是怎么回事?

回答

6

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 }} 
只需使用 set