a minimalist python html template lib
Project description
kTemplate
a minimalist python html template
Quick Start
Installation
pip install kTemplate
Examples
from kTemplate import (
div, img, # common html elements
element # for creating custom element
)
# create common html element
# `class` represents by `cls` due to python keyword
html_str = div(img(src='url'), cls='bar')
# <div class="bar"><img src="url"/></div>
# create custom element
my_element = element(tag="MyElement", content="foo" props="bar")
# <MyElement props="ar">foo</MyElement>
Please refer to the docs for creating HTML templates and components
Documentation
Read the documentation for more about:
- usage details
- function references
- contributing
- testing
- changelog
Motivation
When building web apps with python, no matter using Flask, FastAPI or Django, the go-to template is Jinja. This is a sensible choice when building web app. However it's a bit over-kill when creating simple website. Also, I am not a fan of Jinja's template syntax, eg. putting python loops in html with {% ... %}
looks clumsy:
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
I prefer something like this instead:
ul(
id = "navigation",
content = [
li(
a(item.caption, href=item.href)
)
for item in navigation
]
)
Its pure python, having full support of intellisense, type checking, and all language supports from the text editor. I feel much better DX with this approach.
Separation of concern sounds good, but it comes with a cost: adding another concern 🤪... So separating HTML with python is not always a good choice, especially in simple projects.
Mixing template logic within python eliminates the extra layer of complexity, which I think is a reasonable choice for small/medium size projects. There are libraries provide in-python html template, such as Dominate and fast-html.
Dominate is a well designed lib and I'll certainly go for it for medium sized project. Their with element_name
pattern is a brilliant use of python context manager, highly recommended 👍. However, for simple project, I'm still looking for a simpler solution.
fast-html
come close to what I want. It uses python generator
as element output to speed up the template concatenation process. This is a efficient technical choice, and I think that's why the author name it "fast" html. But still, when dealing with simple or even single page demo sites, pure text elements is what I am looking for instead of generator. Performance hit is negligible in that case.
That's why I create this text centric html template library, and share it on PyPi. I name it "k" template because... just after my initial 😜. Hope u find it useful.
Need Help?
Open a github issue or ping me on Twitter
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for ktemplate-0.3.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56abd81d217fdf0d4528b20628b9fe896ae3c413e3e0b21d01be6a1ce0f10bdc |
|
MD5 | b19ad9a273b84f523bee0e993408fa7f |
|
BLAKE2b-256 | 11b9df7777a450a055e41dff50c1fd51ae6f1823fcac575a7e4c5c41adca3156 |