2012-08-15 119 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