2014-12-21 37 views
0

我有一個相當簡單的設置。我創建了一個燒瓶應用程序,並在路由'/'上調用index.html。燒瓶+ html - 發送數據到擴展模板

的index.html包含此:

{% extends "template.html" %} 

{% block content %} 

    <h4 class="centeredText"> 
     HEADER TEXT 
    </h4> 

    {% for p in paragraph %} 
     <p class="centeredText"> 
      {{ p }} 
     </p> 
    {% endfor %} 

{% endblock %} 

我template.html也很簡單:

<html> 

    <link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='bootstrap.min.css') }}"> 
    <link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='custom.css') }}"> 
    <link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='custom_navbar.css') }}">  
    <link href='http://fonts.googleapis.com/css?family=Josefin+Sans:300' rel='stylesheet' type='text/css'> 

    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> --> 

    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <head> 
     <h1>{% if pageType = 'home' %} HOME {% endif %}</h1> 
    </head> 

    <body> <h1>TEMPLATE</h1> 
     <div class="container"> 
     {% block content %} 
     {% endblock %} 
     </div> 
    </body> 
</html> 

測試時我得到這個錯誤:

expected token 'end of statement block', got '=' 

編輯:錯過了在template.html頭文件中包含變量。我現在知道,成爲錯誤的來源。無論如何要傳遞一個變量到template.html,還是不是很好的做法?

幫助!

+1

一個問題也許是多餘的左括號here' {{%塊內容%}'。雖然當我在我的一個模板上測試語法錯誤時,我得到了一個不同的錯誤。 – Gohn67

+0

@ Gohn67。編輯template.html - 我錯過了傳遞的變量。我現在想它是一個不同的問題:( - 如何將數據傳遞到擴展的template.html文件?這與使用引導的導航欄並使某些元素處於活動狀態 –

+0

我認爲這裏有一個語法錯誤: '{%if pageType ='home'%}',應該是'==' – Gohn67

回答

1

只要您將它傳遞到render_template函數,pageType變量應該出現在您的template.html中。你的情況,你有一個語法錯誤的位置:

{% if pageType = 'home' %} 

您需要使用==代替,所以它應該是這樣的:

{% if pageType == 'home' %}