寻觅生命中的那一片浅草......

[zz]尝试PyTenjin --号称最快的Python 模板库

官网在这里http://www.kuwata-lab.com/tenjin/

曾经使用过Kid、Cheeth、Web2py的模 板,目前使用Mako和Django(Jinjia)。曾经非常的欣赏Web2py的模板,无论代码还是实际的html代码对coder都异常的友好,符 合自己‘想当然’这个库评价标准。但是因为Web2py没有将他的模板独立(好像后来,Limodou的Uliweb将这个库改造后独立了出来),且尚确 是不少功能,就放弃了。

今天看到PyTenjin又有了那种感觉:).

好了,来看代码,

<?py # -*- coding: utf-8 -*- ?>
<?py
from setting import *
from tenjin.helpers.html import *
?>
<?xml version=”1.0″ encoding=”utf-8″?>
<feed xmlns=”http://www.w3.org/2005/Atom”>
<title>#{ SITE_TITLE }</title>
<subtitle>#{SITE_SUB_TITLE}</subtitle>
<link rel=”alternate” type=”text/html” href=”http://#{curdomain}/” />
<link rel=”self” type=”application/atom+xml” href=”http://#{curdomain}/index.xml” />
<id>http://#{curdomain}/</id>
<updated>#{site_updated}</updated>
<rights>Copyright © 2010, #{ SITE_TITLE }</rights>
<?py for topic in posts: ?>
<entry>
<title>${ topic.title}</title>
<link rel=”alternate” type=”text/html” href=”http://#{curdomain}/p/#{topic.key().id()}” />
<id>tag:#{curdomain},#{topic.created.strftime(“%Y-%m-%d)”) }}:/p/#{topic.key().id()}</id>
<published>#{topic.created.strftime(“%Y-%m-%dT%H:%M:%SZ”)}</published>
<updated>#{topic.last_modified.strftime(“%Y-%m-%dT%H:%M:%SZ”)}</updated>
<author>
<name>#{topic.author_name}</name>
<uri>http://#{curdomain}/member/#{topic.author_name}</uri>
</author>
<content type=”html” xml:base=”http://#{curdomain}/” xml:lang=”en”><![CDATA[
#{urlize(myimg(nl2br(topic.short_con)))}
]]></content>
</entry>
<?py #endfor ?>
</feed>

是不是很想当然!

1,单行语句python代码 <?py  xxxxx  ?>

2,块Block语句 python代码

<?py if xxxx?>

XXXXX

<?py #endif?>

————————————————

<?py for xxx ?>

<?py #endfor?>

3, 变量 #{}不含HTML Escape转换 和 ${} 含HTML Escape转换

4, 条件判断表达式必须和if, while ..等在一行写

5,Python代码的缩进必须是4个空格

6, <?PY ?>里是预编译的语句,这些语句只会被执行一次,里面的变量和函数不能被<?py ?>、#{}和${}所用,也不能使用<?py ?>和模板参数中的变量和函数。
7, #{{}}和${{}}用法和#{}、${}类似,但只能使用<?PY ?>中的变量和函数。
8, 子模板可以用include()函数载入。

调用生成hmtl字串和习惯很相象

engine = tenjin.Engine(path=[os.path.join(‘template’, THEME), ‘template’], cache=tenjin.MemoryCacheStorage())
return engine.render(template, context, globals, layout)

原文在:http://hi.baidu.com/ghiewa/item/2f4ecf300306d3b8623affec

尚无评论

发表评论

2024年四月
« 5月    
1234567
891011121314
15161718192021
22232425262728
2930