kTemplate 0.5.1

Creator: bradpython12

Last updated:

Add to Cart

Description:

kTemplate 0.5.1

kTemplate


pythonic way to create HTML/XML/SVG


create tags in pure python
use context manager for tag hierarchy
no external dependencies
read the docs

Quick Start
Installation: pip install kTemplate
from kTemplate import div, img, form, label, input, del_
from kTemplate import Tag # for creating custom element

# === html element ===
tag = div(img(src="url"), id="bar")
print(tag) # <div id="bar"><img src="url"/></div>

# === custom element ===
my_tag = Tag("MyTag", child="foo", attr="bar")
print(my_tag) # <MyTag attr="bar">foo</MyTag>

# == ⭐️ context manager ⭐️ ==
with form() as f:
label("foo", for_="bar") # python keyword 'for' -> 'for_'
input(None, name="bar", type="checkbox", value="baz")

print(f.pretty())
# <form>
# <label for="bar">foo</label>
# <input name="bar" type="checkbox" value="baz"/>
# </form>

# === add content and attributes to existing tag ===
# position args -> attribute w/o value
# python keyword 'class' -> 'class_'
tag = div(class_="foo")
# python keyword 'del' -> 'del_'
tag.add(del_("bar"), "m-2", "rounded", id="baz")
print(tag)
# <div m-2 rounded class="foo" id="baz"><del>bar</del></div>

more examples could be found on references and tests
Limitations

python keywords

tag attributes: class -> class_; for -> for_
tag name: del -> del_


pretty() method doesn't support attribute w/o value

eg. use kwargs selected="" instead of positional args selected



Motivation
When working with HTML, instead of separating python and template files like this:
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>

I prefer a pythonic approach like this:
with ul(id="navigation") as nav:
for item in navigation:
li(a(item.caption, href=item.href))

It provides full intellisense, type checking, and all language supports from the text editor. A much better DX.
Need Help?
github issue
posts

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.