1、靜態文件
static_path
app = tornado.web.Application(
[(r"/", IndexHandler),
],
static_path = os.path.join(os.path.dirname(__file__),"statics"),
debug=True)
StaticFileHandler --提供靜態資源文件的handler
current_path =os.path.dirname(__file__)
app = tornado.web.Application(
[(r"/", IndexHandler),
(r"/(.*)",StaticFileHandler, {"path":os.path.join(current_path,"statics/html"), default_filename:"index.html"}), --這個要配置到最後一個
],
static_path = os.path.join(current_path ,"statics"),
debug=True)
2、使用模板
current_path =os.path.dirname(__file__)
app = tornado.web.Application(
[(r"/", IndexHandler),
(r"/(.*)",StaticFileHandler, {"path":os.path.join(current_path,"statics/html"), default_filename:"index.html"}), --這個要配置到最後一個
],
static_path = os.path.join(current_path ,"statics"),
template_path = os.path.join(current_path ,"template"),
debug=True)
2-1 模板語法
a.變量與表達式
模板中使用 {{}}作惡日變量或者表達式的佔位符
使用render渲染後佔位符{{}}會被替換爲對應的結果值
b.控制語句
控制語句用{\%和\%}包圍
例如:
{% if page is None%} ...{% elif len(str) == 3 %}... {% else ... %}... {% end %}
{% for ... in ... %} ...{% end %}
{%while ... %}... {% end %}
c.函數
static_url()生成靜態目錄下的url
<link rel="stylesheet" href="{{ static_url('style.css')}}">
可以渲染出類似下面的代碼:
<link rel="stylesheet" href="static/style.css/?v=ab13">
static_url()對應的靜態路徑可以修改配置 static_url_prefix的值
轉義:tornado模板中默認開啓轉義, autoescape=None -- 關閉轉義(全局關閉)
關閉某個頁面的轉義, 在html的<body> {% autoescape None %}</body>
某一句話不轉義,按照原始輸出, {% raw text%}
已關閉全局轉義,單據開啓某句話轉義{ autoescape (text){}}
自定義函數: 可以傳入模板
d.塊
{% block%}