htbuilder

0.9.0 · active · verified Thu Apr 16

htbuilder is a Python library for building HTML strings using a purely functional syntax, akin to JSX rather than traditional templating engines. It allows developers to construct HTML elements and attributes using Python functions, providing a clean and programmatic way to generate markup. The library is currently active, with its latest version being 0.9.0, released in September 2023.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create basic HTML elements, add text content, include attributes, and nest elements using `htbuilder`'s functional syntax. It shows both direct tag imports and the `H` factory for common tags.

from htbuilder import div, p, a, H

# Create a simple div with text
hello_world = div('Hello, World!')
print(hello_world.render())
# Expected: <div>Hello, World!</div>

# Create an element with attributes and children
link_element = a(href='https://github.com/tvst/htbuilder')('htbuilder GitHub')
paragraph_element = p('Visit the ', link_element, ' for more info.')

# Nest elements using the H (HtmlElement) factory for common tags
page_content = H.div(id='main-content')(
    H.h1('Welcome'),
    paragraph_element
)

print(page_content.render())

view raw JSON →