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 interal DSL vs external template languages can be found here.

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'HTML Node 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.3.tar.gz (7.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for HtmlNode-0.1.3.tar.gz
Algorithm Hash digest
SHA256 81acafb7672ee69e206016a88e6273d01106e56fddd013535b466d44e42060aa
MD5 079bd978aa409cedb3d8207188c0e474
BLAKE2b-256 fc47037a7a9c2d75111cbb8039d4b021b29f7277f5fe7e008220701500c0bd85

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