Skip to main content

A simple Python HTML generator

Project description

HtmlNode

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>

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

HtmlNode-0.1.7.tar.gz (8.1 kB view details)

Uploaded Source

File details

Details for the file HtmlNode-0.1.7.tar.gz.

File metadata

  • Download URL: HtmlNode-0.1.7.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for HtmlNode-0.1.7.tar.gz
Algorithm Hash digest
SHA256 92823536b743a9300d91e255d024f691f1527146614fef44f73fb42a1eaf28ec
MD5 99dca32be54d68103e67ffa0f50ef38c
BLAKE2b-256 bb1458d725c2cf6925ccc9cb65491c4147a7f3e0debb6766e5cb8cb95ec0032e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page