2017-08-25 46 views
0

在燒瓶中如何做到這一點?燒瓶:如何在圖片的網址中設置條件

我有URL /ninja/<color> 這使得ninja.html ,如果顏色不是紅色,藍色,紫色,或橙,它設置可變顏色「notvalid」

,並根據顏色,我希望此圖像在模板中更改。

<center><img src={{url_for('static', filename= 
     {% if color=="blue" %} 
     'leonardo.png' 
     {% elif color=="purple" %} 
     'donatello.png' 
     {% elif color=="red" %} 
     "raphael.png" 
     {% elif color=="orange" %} 
     ''michelangelo.png" 
     {% elif color=="notvalid" %} 
     "notapril.png" 
     {% else %} 
     'tmnt.png' 
     {% endif %} 
    )}}></center> 

但它讓我一個無效的語法錯誤,「意想不到%」

+2

'「」 michelangelo.png'「 - 這是一個錯字如果不是,這會導致一個語法錯誤 – thaavik

+0

你應該努力去適應?無論是單引號還是雙引號 – gffbss

回答

-1

你可以很容易地計算你打電話render_template

images = { 
"blue": "leonardo.png", 
"purple": "donatello.png", 
... 
} 

render_template("template", image=images["color"], ...) 

在此之前只是做<img src={{url_for('static', filename=image)/> - 神社是非常強大的,但它不會讓你做什麼

+0

這裏顯然有一個錯字,導致語法錯誤。 – 0decimal0

-1

你在上面的模板錯字

''michelangelo.png" #you should use either single or double quotes but not both.