Skip to content

Instantly share code, notes, and snippets.

@starrify
Created June 4, 2013 13:36
Show Gist options
  • Save starrify/5705972 to your computer and use it in GitHub Desktop.
Save starrify/5705972 to your computer and use it in GitHub Desktop.
关于SimpleTemplate Engine的一点简介. for Zhu Mengying
文档参见 http://bottlepy.org/docs/dev/stpl.html
如下是那个SimpleTemplageEngine的一个示例
如下文件被保存在template/flight/oneway.tpl 其中单个百分号开头的行是内嵌的python代码
----start of template/flight/oneway.tpl-------
Oneway flight~<br>
<div id="flights">
%if flights:
% for item in flights:
<div><strong>{{item['fn']}}</strong> : {{item}}</div>
% end
%end
</div>
end of oneway flight~<br>
----end of template/flight/oneway.tpl-------
而在控制view的Python代码中会做的事情请参见下面这个样例:
----start of piece of sample python code--------
@app.get('/flight/oneway')
@bottle.view(config.template_path + 'flight/oneway.tpl')
def oneway_get():
d = {
"a1": "Airport 1",
"a2": "Airport 2",
"dt1": "Datetime 1",
"dt2": "Datetime 2",
"fn": "FlightNo",
}
return {'flights': [d, d]}
----end of piece of sample python code--------
即返回一个包含"flights"项的dict(可以认为就是JSON..)供template engine去渲染.
结果页面为如下的样子. 不过里面那些特殊符号都是用转义做的 所以看上去很乱...
-----start of the resulting page------
Oneway flight~<br>
<div id="flights">
<div><strong>FlightNo</strong> : {&#039;dt1&#039;: &#039;Datetime 1&#039;, &#039;a2&#039;: &#039;Airport 2&#039;, &#039;a1&#039;: &#039;Airport 1&#039;, &#039;dt2&#039;: &#039;Datetime 2&#039;, &#039;fn&#039;: &#039;FlightNo&#039;}</div>
<div><strong>FlightNo</strong> : {&#039;dt1&#039;: &#039;Datetime 1&#039;, &#039;a2&#039;: &#039;Airport 2&#039;, &#039;a1&#039;: &#039;Airport 1&#039;, &#039;dt2&#039;: &#039;Datetime 2&#039;, &#039;fn&#039;: &#039;FlightNo&#039;}</div>
</div>
end of oneway flight~<br>
-----end of the resulting page--------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment