說明:模板文件就是按照必定的規則書寫的展現效果的HTML文件 模板引擎就是負責按照指定規則進行替換的工具css
模板引擎選擇jinja2html
render_template()python
render_templates_string()flask
實例bootstrap
@app.route('/') def index(): #將模板內容響應給用戶 return render_template('index.html') #渲染一內容響應給用戶 return render_template_string('<h1 style="color:green;font-size:18px;">原諒色</h1>')
模板中只存在倆種語法segmentfault
變量app
{{ var }}ide
#像模板文件中傳參 return render_template('index.html',title='首惡') {{ title }}
{% 標籤名 %}函數
注意:工具
在模板中使用字典中的鍵 須要像使用對象得方式來調用 {{data.key}}
若是在模板中給定的變量不存在 則插入的是空字符串 不會報錯
過濾器使用管道符 | 來使用的
default 設置默認值
只有當給定的變量不存在時 則執行默認值
當設置default的boolean的時候 會執行默認值
<li>{{ data.bool|default('我是默認值',boolean=True) }}</li>
format: 字符的格式化
<li>{{ '我叫%s 我今年%d歲了 個人存款爲 %.2f'|format('羅鐵漢',38,23) }}</li>
join: 拼接成字符串
<li>{{ [1,2,3,4]|join('') }}</li> <li>{{ [1,2,3,4]|join('x') }}</li>
replace 替換
<li>{{ data.string|replace('a','x') }}</li>
{{ data.html|striptags }}
實例
{% if data.bool %} <li>{{ data.bool }}值爲真</li> {% elif True %} <li>{{ True }}職位真</li> {% else %} <li>{{ data.bool }}值爲假</li> {% endif %}
實例
{% for i in data.xxxx %} {# 錯誤的迭代方法TypeError: 'bool' object is not iterable #} {# {% for i in data.bool %}#} <li>{{ i }}</li> {% else %} <li>當迭代的變量不存在時 則執行else</li> {% endfor %}
注意:
break continue 不可以在這裏使用
{% for k,v in data.items() %} <li>{{ k }}=>{{ v }}</li> {% endfor %}
變量 | 描述 |
---|---|
loop.index | 獲取當前迭代的索引 從1開始 |
loop.index0 | 獲取當前迭代的索引 從0開始 |
loop.first | 是否爲第一次迭代 |
loop.last | 是否爲最後一次迭代 |
loop.length | 迭代的長度 |
{# 多行註釋 #}
至關於把一個文件 拷貝到當前的你的包含的位置
實例
{% include 'common/header.html' %} <div>我是中間的內容</div> {% include 'common/footer.html' %}
注意:
導入的時候 若是文件和在同一級別 直接導入就能夠 若是包含在某個目錄中 須要寫出路徑
{% include 'common/header.html' %} {% include 'test.html' %}
概念: 相似python中的函數
實例
在macro.html中
{% macro input(name,type='text',value='') %} <input type="{{ type }}" name="{{ name }}" value="{{ value }}"> {% endmacro %}
宏的調用
{{ input('text','username','') }} {{ input() }} {{ input(type='password',name='userpass') }}
{% import 'test.html' as test %} {% import 'common/test.html' as test %} <p>用戶名: {{ test.input(type='password',name='userpass') }}</p>
{% from 'test.html' import input %} {% from 'common/test.html' import input %} <p>用戶名: {{ input(type='password',name='userpass') }}</p>
注意:
<!DOCTYPE html> <html lang="en"> <head> {% block header %} <meta charset="UTF-8"> {% block meta %} {% endblock %} <title>{% block title%}首頁{% endblock %}</title> <style> {% block style %} p{color:red;} {% endblock %} </style> {% block link %} {% endblock %} {% block script %} {% endblock %} {% endblock %} </head> <body> <header>頭部</header> {% block con %} 我是中間的內容部分 {% endblock %} <footer>尾部</footer> </body> </html>
{% extends 'common/base.html' %} {% block title %} 個人首頁 {% endblock %} {% block style %} {{ super() }} p{color:green;} {% endblock %} {% block con %} <p>我是首頁的內容</p> 我是首頁的內容 {% endblock %}
注意:
若是當替換某個樣式的時候 全部原來的樣式 都消失了 去查看是否使用了super()
安裝
pip install flask-bootstrap
sudo pip3 install flask-bootstrap
使用
繼承 bootstrap/base.html 基礎模板 改形成適用於本身網站的base.html基礎模板
{% extends 'bootstrap/base.html' %} {% block navbar %} <nav class="navbar navbar-inverse" style="border-radius: 0px;"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">首頁 <span class="sr-only">(current)</span></a></li> <li><a href="#">發帖子</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <form class="navbar-form navbar-left"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> <li><a href="#">註冊</a></li> <li><a href="#">登陸</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">我的中心 <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">我的信息</a></li> <li><a href="#">修改頭像</a></li> <li><a href="#">修改密碼</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> <li role="separator" class="divider"></li> <li><a href="#">退出登陸</a></li> </ul> </li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> {% endblock %} {% block content %} <div class="container"> {% block pagecontent %} 網頁的中間內容部分寫在當前的位置 {% endblock %} </div> {% endblock %}
使用 index.html
{% extends 'common/base.html' %} {% block title %} 首頁 {% endblock %}
@app.errorhandler(404) def page_not_found(e): return render_template('common/error.html',error=e,code=404) @app.errorhandler(500) def server_error(e): return render_template('common/error.html',error=e,code=500)
{% extends 'common/base.html' %} {% block title %} {{ code }}錯誤 {% endblock %} {% block pagecontent %} <div class="alert alert-danger" role="alert">{{ error }}</div> {% endblock %}
@app.route('/') def index(): return render_template('index.html',arg1=1,arg2=2...)
@app.route('/') def index(): return render_template('index.html',arg={arg1:1,arg2:2...}) kwarg={arg1:1,arg2:2...} return render_template('index.html',``)
@app.route('/') def index(): g.name = '張三' g.age = 18 return render_template('index.html') 模板中 <ol> <li>{{ g.name }}</li> <li>{{ g.age }}</li> </ol>
@app.route('/') def index(): name = '張三' age = 18 print(locals()) return render_template('index.html',**locals()) 模板中 <li>{{ name }}</li> <li>{{ age }}</li>
@app.route('/test/') def test(): print(url_for('index',_external=True)) return 'test'
靜態資源:圖片,css,js,視頻,音頻,,
實例
<img src="{{ url_for('static',filename='img/17.jpg') }}" alt="">