# kTemplate
[![ci-badge]][ci-url] [![pypi-badge]][pypi-url] [![MIT-badge]][MIT-url] [![black-badge]][black-url]
> 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`
```python
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:
```html
<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:
```python
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?
[![git-logo] github issue][github issue]
[![x-logo] posts][x-post]
[black-badge]: https://img.shields.io/badge/code%20style-black-000000.svg
[black-url]: https://github.com/psf/black
[ci-badge]: https://github.com/hoishing/kTemplate/actions/workflows/ci.yml/badge.svg
[ci-url]: https://github.com/hoishing/kTemplate/actions/workflows/ci.yml
[docs]: https://hoishing.github.io/kTemplate
[git-logo]: https://api.iconify.design/bi/github.svg?color=%236FD886&width=20
[github issue]: https://github.com/hoishing/kTemplate/issues
[MIT-badge]: https://img.shields.io/github/license/hoishing/kTemplate
[MIT-url]: https://opensource.org/licenses/MIT
[pypi-badge]: https://img.shields.io/pypi/v/ktemplate
[pypi-url]: https://pypi.org/project/ktemplate/
[references]: https://hoishing.github.io/kTemplate/references
[tests]: https://github.com/hoishing/kTemplate/tree/main/tests
[x-logo]: https://api.iconify.design/ri:twitter-x-fill.svg?width=20&color=DarkGray
[x-post]: https://x.com/hoishing
Raw data
{
"_id": null,
"home_page": "https://hoishing.github.io/kTemplate",
"name": "kTemplate",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.12",
"maintainer_email": null,
"keywords": "template, html",
"author": "Kelvin Ng",
"author_email": "hoishing@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e7/bc/07be46aff3ecbbaf7862eda95d4135ae364be9be5f8f0d7cdceaf495e2be/ktemplate-0.5.1.tar.gz",
"platform": null,
"description": "# kTemplate\n\n[![ci-badge]][ci-url] [![pypi-badge]][pypi-url] [![MIT-badge]][MIT-url] [![black-badge]][black-url]\n\n> pythonic way to create HTML/XML/SVG\n\n- create tags in pure python\n- use context manager for tag hierarchy\n- no external dependencies\n- read the [docs]\n\n## Quick Start\n\nInstallation: `pip install kTemplate`\n\n```python\nfrom kTemplate import div, img, form, label, input, del_\nfrom kTemplate import Tag # for creating custom element\n\n# === html element ===\ntag = div(img(src=\"url\"), id=\"bar\")\nprint(tag) # <div id=\"bar\"><img src=\"url\"/></div>\n\n# === custom element ===\nmy_tag = Tag(\"MyTag\", child=\"foo\", attr=\"bar\")\nprint(my_tag) # <MyTag attr=\"bar\">foo</MyTag>\n\n# == \u2b50\ufe0f context manager \u2b50\ufe0f ==\nwith form() as f:\n label(\"foo\", for_=\"bar\") # python keyword 'for' -> 'for_'\n input(None, name=\"bar\", type=\"checkbox\", value=\"baz\")\n\nprint(f.pretty())\n# <form>\n# <label for=\"bar\">foo</label>\n# <input name=\"bar\" type=\"checkbox\" value=\"baz\"/>\n# </form>\n\n# === add content and attributes to existing tag ===\n# position args -> attribute w/o value\n# python keyword 'class' -> 'class_'\ntag = div(class_=\"foo\") \n# python keyword 'del' -> 'del_'\ntag.add(del_(\"bar\"), \"m-2\", \"rounded\", id=\"baz\") \nprint(tag) \n# <div m-2 rounded class=\"foo\" id=\"baz\"><del>bar</del></div>\n```\n\nmore examples could be found on [references] and [tests]\n\n## Limitations\n\n- python keywords\n - tag attributes: `class` -> `class_`; `for` -> `for_`\n - tag name: `del` -> `del_`\n- `pretty()` method doesn't support attribute w/o value\n - eg. use kwargs `selected=\"\"` instead of positional args `selected`\n\n## Motivation\n\nWhen working with HTML, instead of separating python and template files like this:\n\n```html\n<ul id=\"navigation\">\n {% for item in navigation %}\n <li><a href=\"{{ item.href }}\">{{ item.caption }}</a></li>\n {% endfor %}\n</ul>\n```\n\nI prefer a pythonic approach like this:\n\n```python\nwith ul(id=\"navigation\") as nav:\n for item in navigation:\n li(a(item.caption, href=item.href))\n```\n\nIt provides full intellisense, type checking, and all language supports from the text editor. A much better DX.\n\n## Need Help?\n\n[![git-logo] github issue][github issue]\n\n[![x-logo] posts][x-post]\n\n[black-badge]: https://img.shields.io/badge/code%20style-black-000000.svg\n[black-url]: https://github.com/psf/black\n[ci-badge]: https://github.com/hoishing/kTemplate/actions/workflows/ci.yml/badge.svg\n[ci-url]: https://github.com/hoishing/kTemplate/actions/workflows/ci.yml\n[docs]: https://hoishing.github.io/kTemplate\n[git-logo]: https://api.iconify.design/bi/github.svg?color=%236FD886&width=20\n[github issue]: https://github.com/hoishing/kTemplate/issues\n[MIT-badge]: https://img.shields.io/github/license/hoishing/kTemplate\n[MIT-url]: https://opensource.org/licenses/MIT\n[pypi-badge]: https://img.shields.io/pypi/v/ktemplate\n[pypi-url]: https://pypi.org/project/ktemplate/\n[references]: https://hoishing.github.io/kTemplate/references\n[tests]: https://github.com/hoishing/kTemplate/tree/main/tests\n[x-logo]: https://api.iconify.design/ri:twitter-x-fill.svg?width=20&color=DarkGray\n[x-post]: https://x.com/hoishing\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "pythonic way to create HTML/XML/SVG",
"version": "0.5.1",
"project_urls": {
"Documentation": "https://hoishing.github.io/kTemplate",
"Homepage": "https://hoishing.github.io/kTemplate",
"Repository": "https://github.com/hoishing/kTemplate"
},
"split_keywords": [
"template",
" html"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d8c2ef1c724f03fbc90122409f18315b27ff44157b43f72ccadc11259d63ce2b",
"md5": "bdabbdd111a87df98613a0ce559b0d2f",
"sha256": "cb489718a64d4b74d931aff412855e3be55b03f83b7d61fc682a96195a46ef1b"
},
"downloads": -1,
"filename": "ktemplate-0.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bdabbdd111a87df98613a0ce559b0d2f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.12",
"size": 8268,
"upload_time": "2024-08-17T05:08:11",
"upload_time_iso_8601": "2024-08-17T05:08:11.851077Z",
"url": "https://files.pythonhosted.org/packages/d8/c2/ef1c724f03fbc90122409f18315b27ff44157b43f72ccadc11259d63ce2b/ktemplate-0.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e7bc07be46aff3ecbbaf7862eda95d4135ae364be9be5f8f0d7cdceaf495e2be",
"md5": "2b3e91f323d6ad33fe8c6408d45af9ba",
"sha256": "a32d2ddd9154f2730442128552d267660141f0fb5235a29bb3a435fcb2484e97"
},
"downloads": -1,
"filename": "ktemplate-0.5.1.tar.gz",
"has_sig": false,
"md5_digest": "2b3e91f323d6ad33fe8c6408d45af9ba",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.12",
"size": 7289,
"upload_time": "2024-08-17T05:08:12",
"upload_time_iso_8601": "2024-08-17T05:08:12.842496Z",
"url": "https://files.pythonhosted.org/packages/e7/bc/07be46aff3ecbbaf7862eda95d4135ae364be9be5f8f0d7cdceaf495e2be/ktemplate-0.5.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-17 05:08:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hoishing",
"github_project": "kTemplate",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ktemplate"
}