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

katagami的Python项目详细描述


这个库是许多Python templating libraries库之一。

功能

示例

inline Python expression和python的Block structure)生成一个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>

默认情况下,此示例引发异常,计算表达式必须是 strunicode在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”字符串功能:

>>> myprint(render_string('''<?py
...         from katagami import cast_string
...     ?><html><body>
...     <?=1?>
... </body></html>'''))
<html><body>
    1
</body></html>

还可以设置除挂钩之外的功能:

>>> 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>

  • ‘<? }’ and ‘{ ?>’ are wrong. Don’t insert space. ‘<?}’ and ‘{?>’ are correct.
  • Ending colon (‘:’) is required.
  • Block closing ‘<?}?>’ is required.

编码检测

将自动检测编码:

>>> 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 improve backward compatibility of the test
  • 2.0.0 change a lot and add some features
  • 1.1.0 change api, add except_handler, add shorthand of gettext (<?_message?>),
    some fixes
  • 1.0.3 fix ignoring encoding argument, fix indent bug, add renderString
  • 1.0.2 improve doctest compatibility, some fixes
  • 1.0.1 fix bugs, docs, speed
  • 1.0.0 remove backward compatibility

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java DB2查看最近执行的命令   java正则表达式如何只匹配数字后的字符,而不在匹配模式中包含数字   java是否可以使用Jmh运行基于时间的预热阶段?   java必须输入两次输入,扫描仪才能读取   java如何使用一个或多个类方法设置多个类字段,而这些类方法并不专门引用任何字段?   java Quartz的CronTrigger每24小时一次,如午夜   java字符串索引超出边界异常?   java FXMLLoader找不到fxml文件Maven项目   java Eclipse Indigo在安装m2ewtp插件时遇到问题   java如何为连接池配置Hibernate、Spring和ApacheDBCP?   java netbeans:类中的类路径。福奈姆   javajmx及其在Tomcat内部Docker上的调试   java HTTP状态404 tomcat 7   Java:String split():我希望它在末尾包含空字符串   java我应该使用枚举集吗?   Java StringTokenizer如何查找段落结尾?