HtmlNode is an internal Domain Specific Language (DSL) using Python to generate HTML templates. It is designed to be really easy and flexible to use. By using DSL instead of a specific templating language, you can write Python code to render HTML directly, so you can debug your presentation logics easily. Also, you get the full power of Python in writing presentation logic, without the hassle to learn another templating language.
An overview for using internal DSL vs external template languages can be found here.
Installation
Automatic installation:
pip install HtmlNode
HtmlNode is listed in PyPI and can be installed with pip or easy_install.
Manual installation: Download the latest source from PyPI.
tar xvzf HtmlNode-$VERSION.tar.gz cd HtmlNode-$VERSION sudo python setup.py install
The HtmlNode source code is hosted on GitHub.
Prerequisites: HtmlNode only runs on Python 2.7 currently.
Features
Very simple syntax and natural way to generate HTML
Unicode support
Auto-escape dangerous characters
Template inheritance support
Context variables insertion into templates
Auto-completion support for IDEs which can introspect
Custom HTML elements can be created on the fly
Flexible ways to include attributes for HTML element
Usage Example
Example 1 - Simple HTML:
Code snippet:
from html_node import html_node as h
template = h.div(class_='container')(
h.span('Hello World!', style='color: pink; font-weight: bold;')
)
print template
Output:
<div class="container">
<span style="color: pink; font-weight: bold;">
Hello World!
</span>
</div>
By default, the actual output is concatenated without space or line feeds.
Example 2 - Simple reusable layout (with inheritance and placeholders)
You can use placeholder to mark different block section to replace with. You give the placeholder a name, and provide a method with the same name that returns a unicode string. You can inherit the layout just like how you normally do in Python.
Code snippet:
from html_node import html_node as h, BaseTemplate, placeholder
class MyLayout(BaseTemplate):
"""My customized layout which other templates can inherit from."""
title = 'HtmlNode'
def template(self):
return h.html5(placeholder('head'), placeholder('body'))
@placeholder
def head(self):
return h.head(
h.title(self.title),
h.link(rel="stylesheet", type="text/css", href="default.css"),
)
@placeholder
def body(self):
return h.body(
h.div(class_=['container', 'expand'])(
placeholder('content')
)
)
@placeholder
def content(self):
raise NotImplementedError
class MyViewTemplate(MyLayout):
"""This template will provide the content block for the layout."""
@placeholder
def content(self):
return u'HtmlNode is a flexible and easy-to-use HTML generator!'
template = MyViewTemplate()
print template
Output:
<!DOCTYPE HTML>
<html>
<head>
<title>HtmlNode</title>
<link href="default.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container expand">
HtmlNode is a flexible and easy-to-use HTML generator!
</div>
</body>
</html>
Metadata
Release files for HtmlNode 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| HtmlNode-0.1.8.tar.gz | 8.1 kB | Without provenance | Details for HtmlNode-0.1.8.tar.gz |
Release files / HtmlNode-0.1.8.tar.gz
| Download URL | HtmlNode-0.1.8.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
190b90c362a247a35f7e50318fe5beb644ab376a4bf6ea7d0e2317c55f6f0a69
|
|
BLAKE2b-256 checksum How to use checksums |
bdecc20615c0780ebd49823852f6a00f66699c30ceb112df1e8d80bad1bfd18c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for HtmlNode 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.