Skip to main content

This library is one of many Python templating libraries.

Features

Example

Make a HTML string with inline Python expression and Python’s for (Block structure):

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

Inline Python expression

This feature evaluates your inline expression and output to result:

>>> myprint(render_string('''<html><body>
...     <?='hello, world'?>
... </body></html>'''))
<html><body>
    hello, world
</body></html>

By the default, this example raises an exception, evaluated expression must be str (unicode in 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

Set the cast_string feature:

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

Also set the except_hook feature:

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

Embed Python script

All indentation will be arranged automatically:

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

Block structure

Indentation with C-style block structure:

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

Note

  • ‘<? }’ and ‘{ ?>’ are wrong. Don’t insert space. ‘<?}’ and ‘{?>’ are correct.

  • Ending colon (‘:’) is required.

  • Block closing ‘<?}?>’ is required.

Encoding detection

Encoding will be detected automatically:

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

Supported formats:

  • <?xml encoding=”ENCODING”?>

  • <meta charset=”ENCODING”>

  • <meta http-equiv=”Content-Type” content=”MIMETYPE; ENCODING”>

History

  • 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

Metadata

Release files for katagami 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for katagami 2.0.1
File Size Uploaded
katagami-2.0.1.zip 11.1 kB Details

Release files / katagami-2.0.1.zip

Download URL katagami-2.0.1.zip
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
74c2cd3f714e5b7ae19f92fa6ef058a2fcb0176db2b567102d8437bb76997b0b
BLAKE2b-256 checksum
How to use checksums
f38914617867dc632070fa86e8f7068c3f3e99fbbe85f10ba01a1a5bd67c80a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

2.0.1 This release

1 release file

2.0.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page