All Projects → sayanarijit → htmldoom

sayanarijit / htmldoom

Licence: MIT license
An intuitive, high performance HTML rendering framework

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to htmldoom

Mikado
Mikado is the webs fastest template library for building user interfaces.
Stars: ✭ 323 (+797.22%)
Mutual labels:  template-engine, rendering-engine
dry
Dry is a new template engine and language, and is a superset of Shopify's Liquid, with first-class support for advanced inheritance features, and more. From the creators of Enquirer, Assemble, Remarkable, and Micromatch.
Stars: ✭ 66 (+83.33%)
Mutual labels:  template-engine, rendering-engine
Yarte
Yarte stands for Yet Another Rust Template Engine
Stars: ✭ 189 (+425%)
Mutual labels:  template-engine
picocog
A tiny code generation library (< 8 KB) written in Java, useful for any purpose, but ideal for JSR-269
Stars: ✭ 82 (+127.78%)
Mutual labels:  template-engine
Eta
Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
Stars: ✭ 233 (+547.22%)
Mutual labels:  template-engine
Ructe
Rust Compiled Templates with static-file handling
Stars: ✭ 206 (+472.22%)
Mutual labels:  template-engine
Sailfish
Simple, small, and extremely fast template engine for Rust
Stars: ✭ 242 (+572.22%)
Mutual labels:  template-engine
Thymeleaf
Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
Stars: ✭ 2,251 (+6152.78%)
Mutual labels:  template-engine
keikan
An elegant (imo) rendering engine written in Rust.
Stars: ✭ 55 (+52.78%)
Mutual labels:  rendering-engine
Himl
HTML-based Indented Markup Language for Ruby
Stars: ✭ 236 (+555.56%)
Mutual labels:  template-engine
abell-renderer
A template engine that lets you write variables, loops, and conditions in HTML using JavaScript Syntax.
Stars: ✭ 42 (+16.67%)
Mutual labels:  template-engine
Pupa
Simple micro templating
Stars: ✭ 231 (+541.67%)
Mutual labels:  template-engine
Fatfree
A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
Stars: ✭ 2,504 (+6855.56%)
Mutual labels:  template-engine
Jade
Jade.go - pug template engine for Go (golang)
Stars: ✭ 251 (+597.22%)
Mutual labels:  template-engine
React Ssr
React SSR as a view template engine
Stars: ✭ 200 (+455.56%)
Mutual labels:  template-engine
liquid
A Python engine for the Liquid template language.
Stars: ✭ 40 (+11.11%)
Mutual labels:  template-engine
Elefant
Elefant, the refreshingly simple PHP CMS and web framework.
Stars: ✭ 188 (+422.22%)
Mutual labels:  template-engine
Jte
jte is a secure and lightweight template engine for Java.
Stars: ✭ 228 (+533.33%)
Mutual labels:  template-engine
Sodajs
Light weight but powerful template engine for JavaScript
Stars: ✭ 240 (+566.67%)
Mutual labels:  template-engine
Kvantum
An intellectual (HTTP/HTTPS) web server with support for server side templating (Crush, Apache Velocity and JTwig)
Stars: ✭ 17 (-52.78%)
Mutual labels:  template-engine

htmldoom

Documentation PyPI version PyPI pyversions Build Status codecov Code style: black

Introducing htmldoom

htmldoom is an HTML rendering framework for Python that helps you define your HTML documents, templates, layouts and contents in an organised way, gives you the flexibility to use many different syntaxes, option to declare and debug different components and pages separately and more... all without making you worry about the runtime performance.

How does it work?

Understanding the working principle will get pretty intuitive if we go one feature at a time. Let's start with HTML escaping.

HTML escaping

In the center of htmldoom's functionality is the HTML escaping logic. Texts to be escaped are represented using str, while texts that should not be escaped are represented using bytes. The htmldoom.renders() function receives the texts as inputs, then for each input, it decides whether to escape it or not based on if it is str or bytes and returns the rendered text in str format.

We can use htmldoom.txt() to convert some string into HTML escaped text, and htmldoom.raw() to convert some string into HTML unescaped text. It should be noted that both the functions will return text formatted in bytes which can be passed to the htmldoom.render() to render it back to str.

HTML elements

HTML elements or tags can be defined using the htmldoom.composite_tag() and the htmldoom.leaf_tag() functions. Any tag that can have child elements (e.g. <p></p>) are defined using htmldoom.composite_tag().

For example:

p = composite_tag("p")

Tags that cannot have child elements are defined using htmldoom.leaf_tag().

For example:

input_ = leaf_tag("input")

So when we call p(class_="red")("foo", "bar") or input_(type_="button"), we get bytes encoded <p class="red">foobar</p> or <input type="button" /> respectively. The htmldoom.render() function will render them back to str.

HTML components / layouts

HTML components i.e. combination of several elements (tags and texts) can be defined using the @htmldoom.renders() decorator like below.

from htmldoom import renders, elements as e

@renders(
    e.p(class_="red")("{key1}", "{key2}"),
    e._input(type_="button")
)
def my_component(value1, value2):
    return {"key1": value1, "key2": value2}

Now we can render the component by calling it with the required arguments e.g.

my_document(value1="&amp;", value2=b"&amp;")

This will return a bytes encoded text. The value1 will be escaped to &amp;amp; since it is str. value2 is in bytes, hence it will remain the same and wil be rendered in the browser as &.

Calling this will return bytes encoded text

<p class="red">foobar</p><input type="button" />

which can be rendered into string using htmldoom.render().

This mechanism renders the template text during module load time. Hence there should be no performance drop in runtime.

NOTE: It is important to note that every { and } should be escaped with {{ and }} when it is not being used as a placeholder for some variable.

It is also possible to define the components using yaml syntax like below.

some:
  demo:
    p:
    - class: red
    - - "{key1}"
      - "{key2}"
    input:
    - type: button

And then define the component renderer like below.

from htmldoom import renders
from htmldoom.yaml_loader import loadyaml

@renders(loadyaml("path/to/component.yaml", ("some", "demo")))
def my_component(value1, value2):
    return {"key1": value1, "key2": value2}

We can also use escaped and raw loaders with the htmldoom.loadtxt() and htmldoom.loadraw() functions respectively. We only need to pass the file path as shown in the yaml example.

We can use the same syntax to define reusable layouts.

Separating values from templates

htmldoom provides us with a friendly way to separate values from layouts or components templates. The htmldoom.value_loader.loadvalues() function scans the given directory and returns a nested namedtuple with the file or directory names as element names and the loaded contents as the values. We can pass this to components like below.

from htmldoom import renders, elements as e
from htmldoom.value_loader import loadvalues

values = loadvalues("path/to/values")

@renders(
    e.p()("{v.title}"),
    e.p()("{v.content.line1}")
    e.p()("{v.content.line2}")
    e.p()("{v.content.line3}")
)
def my_component():
    return {"v": values}

So we need a directory structure like below.

values
├── title.txt
└── content
    ├── line1.txt
    ├── line2.html
    └── line3.yml

As you mught have guessed, this can load the values based of file types. You can use your own renderer for file types such as md or rst by extending the htmldoom.value_loader.EXTENSION_RENDERERS map and passing is to the htmldoom.value_loader.loadvalues() function like below.

from htmldoom import raw
from htmldoom.value_loader import loadvalues, EXTENSION_RENDERERS

def markdown_to_html(path):
    """Some function that reads the file, converts the content to HTML,
    and returns the converted data as string.
    """
    # The logic goes here...

MY_EXTENSION_RENDERERS = dict(
    md=lambda path: raw(markdown_to_html(path)),
    **EXTENSION_RENDERERS
)

values = loadvalues("path/to/values", extension_renderers=MY_EXTENSION_RENDERERS)

In fact, this documentation is generated using the same method.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].