风流秘史2,我不是明星20130930,一起看电影
cheetah简介
cheetah提供了一门简单语言,用来定义提供基本流控制和对象访问构造的模板。它借用了velocity的基本模板语法,但是添加了一些特性,为cheetah模板提供对
python的便利构造了访问。
from cheetah.template import template
dummyuser={'firstname':'harvey','lastname':'liu'}
dummyorder={'id':2}
definition = """hello, $user.firstname.
your number is $order.id."""
definition_new = template(definition, searchlist=[{'user' : dummyuser,'order' : dummyorder}])
definition字符串包含模板定义,它可以对外部变量(动态部分)进行引用。template构造函数在这里用来将模板定义绑定到名称空间的searchlist:查找对象的方式对应于定义中使用的变量。例如,模板定义中的$user在这里映射到dummyuser变量。除此之外,可以提前运行template构造函数,并在准备使用特定对象解释模板的时候设置其searchlist成员。
假设需要更改模板,需要打印用户的全名,而不是用户的名字。假定dummyuser对象已经提供了该信息,则可以通过更改模板定义单独进行此更改。而在使用内置python模板时,则必须更改python代码。
流控制:#if指令
cheetah提供了一个#if指令,该指令允许设置if-then-else条件。
hello, $user.firstname.
your order (#$order.id) has shipped:
#for $purchased, $quantity in $order.purchased.items():
$purchased.name: $quantity unit
#if $quantity != 1
s
#end if
#end for
打印结果为:
widget, green: 50 unit
s
widget, blue: 1 unit
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
python如何查看网页代码
用python查看网页代码的方法:1、使用“import”导入requests包import requests2、使用requests包的get()函数通过网页...
[阅读全文]
-
-
python如何保存文本文件
python保存文本文件的方法:使用python内置的open()类可以打开文本文件,向文件里面写入数据可以用write()函数,写完之后,使用close()函...
[阅读全文]
-
python如何编写win程序
python可以编写win程序。win程序的格式是exe,下面我们就来看一下使用python编写exe程序的方法。编写好python程序后py2exe模块即可将...
[阅读全文]
-
-
-
-
-
-
网友评论