Created
June 4, 2013 13:36
-
-
Save starrify/5705972 to your computer and use it in GitHub Desktop.
关于SimpleTemplate Engine的一点简介. for Zhu Mengying
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
文档参见 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> : {'dt1': 'Datetime 1', 'a2': 'Airport 2', 'a1': 'Airport 1', 'dt2': 'Datetime 2', 'fn': 'FlightNo'}</div> | |
<div><strong>FlightNo</strong> : {'dt1': 'Datetime 1', 'a2': 'Airport 2', 'a1': 'Airport 1', 'dt2': 'Datetime 2', 'fn': 'FlightNo'}</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