跳转到主要内容

katagami:一个简单的xml/html模板库

项目描述

这个库是许多 Python模板库 之一。

功能

示例

使用内联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,&nbsp;
...     <? try: {?>
...         <?=name?>
...     <?} except NameError: {?>
...         NameError
...     <?} else: {?>
...         never output here
...     <?}?>
...     </p>
... </body>
... </html>'''))
<html>
<body>
    <p>hello,&nbsp;
<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 (11.1 kB 查看哈希值)

支持