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%}