katagami:一个简单的xml/html模板库
项目描述
这个库是许多 Python模板库 之一。
功能
基于XML的处理指令 (<?…?>)
简单的功能和简单的实现
XML/HTML内部的Python脚本,任何级别的缩进
支持Python 2和Python 3
与mako一样快
可迭代输出
示例
使用内联Python表达式和Python的for (块结构)创建HTML字符串
>>> from katagami import render_string, myprint >>> myprint(render_string('''<html> ... <body> ... <? for name in names: {?> ... <p>hello, <?=name?></p> ... <?}?> ... </body> ... </html>''', {'names': ['world', 'python']})) <html> <body> <BLANKLINE> <p>hello, world</p> <BLANKLINE> <p>hello, python</p> <BLANKLINE> </body> </html>
内联Python表达式
此功能评估您的内联表达式并将结果输出
>>> myprint(render_string('''<html><body> ... <?='hello, world'?> ... </body></html>''')) <html><body> hello, world </body></html>
默认情况下,此示例会引发异常,评估后的表达式必须是str (unicode在Python 2中)
>>> myprint(render_string('''<html><body> ... <?=1?> ... </body></html>''')) #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... TypeError: Can't convert 'int' object to str implicitly
设置cast_string功能
>>> myprint(render_string('''<?py ... from katagami import cast_string ... ?><html><body> ... <?=1?> ... </body></html>''')) <html><body> 1 </body></html>
还设置except_hook功能
>>> myprint(render_string('''<?py ... from katagami import except_hook ... ?><html><body> ... <?=1?> ... </body></html>''')) <html><body> Can't convert 'int' object to str implicitly </body></html>
嵌入Python脚本
所有缩进都将自动排列
>>> myprint(render_string('''<html> ... <?py ... # It is a top level here. This works fine. ... if 1: ... msg = 'message from indented script' ... ?> ... <body> ... <p><?=msg?></p> ... <?py msg = 'message from single line script' # This works fine too. ?> ... <p><?=msg?></p> ... <? if 1: {?> ... <?py ... # Is is nested here. This also works fine. ... msg = 'message from nested indented script' ... ?> ... <p><?=msg?></p> ... <?}?> ... </body> ... </html>''')) <html> <BLANKLINE> <body> <p>message from indented script</p> <BLANKLINE> <p>message from single line script</p> <BLANKLINE> <BLANKLINE> <p>message from nested indented script</p> <BLANKLINE> </body> </html>
块结构
C样式块结构的缩进
>>> myprint(render_string('''<html> ... <body> ... <p>hello, ... <? try: {?> ... <?=name?> ... <?} except NameError: {?> ... NameError ... <?} else: {?> ... never output here ... <?}?> ... </p> ... </body> ... </html>''')) <html> <body> <p>hello, <BLANKLINE> <BLANKLINE> NameError <BLANKLINE> </p> </body> </html>
注意
‘<? }’ 和 ‘{ ?>’ 是错误的。不要插入空格。‘<?}’ 和 ‘{?>’ 是正确的。
需要结束冒号(‘:’)。
需要块关闭‘<?}?>’。
编码检测
编码将自动检测
>>> myprint(render_string(b'''<html> ... <head><meta charset="shift-jis"></head> ... <body>\x93\xfa\x96{\x8c\xea</body> ... </html>''')) <html> <head><meta charset="shift-jis"></head> <body>\u65e5\u672c\u8a9e</body> </html>
支持的格式
<?xml encoding=”ENCODING”?>
<meta charset=”ENCODING”>
<meta http-equiv="Content-Type" content="MIMETYPE; ENCODING">
历史
2.0.1 提高测试的向后兼容性
2.0.0 改变很多并添加一些功能
- 1.1.0 修改API,添加except_handler,添加gettext的缩写(<?_message?>)
一些修复
1.0.3 修复忽略 encoding 参数,修复缩进错误,添加 renderString
1.0.2 提高doctest兼容性,一些修复
1.0.1 修复错误,文档,速度
1.0.0 移除向后兼容性
项目详情
关闭
katagami-2.0.1.zip的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 74c2cd3f714e5b7ae19f92fa6ef058a2fcb0176db2b567102d8437bb76997b0b |
|
MD5 | 0efa5d640e6bdf6c3a3bae53d8aaaf8a |
|
BLAKE2b-256 | f38914617867dc632070fa86e8f7068c3f3e99fbbe85f10ba01a1a5bd67c80a1 |