Skip to main content

A Python Templating Framework to allow templating logic to be programmed in python.

Project description

UTemplates - Python Templating Engine

UTemplates is a Python templating engine focused on HTML but with the potential to be extended to other languages. It facilitates the creation, manipulation, and rendering of HTML elements programmatically using Python classes and methods.

Table of Contents

Installation

  1. Clone the repository or download the source code.
pip install utemplates

Getting Started

Before you start using UTemplates, ensure you have a Python environment set up. UTemplates supports Python 3.7 and newer.

Usage

Configuration

To customize the behavior of UTemplates, you can modify or provide a utemplates_config.json configuration file. This file allows you to define conversion functions that will be applied to values before rendering them. The path to the configuration file can be set through an environment variable UTEMPLATES_CONFIG_PATH.

Config Format

The configuration file is a JSON file with the following structure:

{
  "conversions": [
    "module1.submodule.function1",
    "module2.submodule.function2"
  ]
}
  • Each string in the "conversions" list is a dot-path to a conversion function that should be imported and applied during rendering.

Creating Elements

UTemplates provides classes to structure HTML content. These can be generic or specific HTML elements:

  • GeneralBaseElement: Abstract base class for all HTML elements.
  • GroupedBaseElement: Class for grouping multiple elements.
  • BaseHTMLElement: Class for creating standard HTML elements.
  • SafeHTMLElement: Class for wrapping pre-processed HTML strings that should not be escaped.

You can create instances of these classes by calling their constructors and passing the appropriate arguments.

Example

from utemplates.html_specific.base import BaseHTMLElement

div_element = BaseHTMLElement("div", {"class": "container"}, "Hello World")

This example creates a <div> element with the class "container" and containing the text "Hello World".

Rendering HTML

To render HTML content, use the provided render method from the rendering module. It takes a string, a list of strings or elements, or a single element and generates an HTML string.

Example

from utemplates.rendering import render

# For a single element
rendered_html = render(div_element())

# For multiple elements
rendered_html = render([div_element(), another_element()])

# You can also save the rendered HTML to a file
from utemplates.rendering import save_to_file
save_to_file(rendered_html, 'output.html')

Utilities

The utils.py module includes utility functions:

  • convert_value: Applies configured conversion functions to a given value.

Integrations

UTemplates offers integration support for the Django framework to render responses directly:

Django Integration

from utemplates.integrations.django_integrations import render_to_response

def my_view(request):
    html_content = create_some_html_content()
    return render_to_response(html_content)

License

UTemplates is open-sourced under the MIT license.

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

utemplates-0.26.tar.gz (37.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

utemplates-0.26-py3-none-any.whl (52.1 kB view details)

Uploaded Python 3

File details

Details for the file utemplates-0.26.tar.gz.

File metadata

  • Download URL: utemplates-0.26.tar.gz
  • Upload date:
  • Size: 37.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for utemplates-0.26.tar.gz
Algorithm Hash digest
SHA256 56a9e4b5196a55a6c5d63f081435fb7f03cf6318ea8c186913d92834e3db0ec1
MD5 085bed1954a567610ea3364b516122f0
BLAKE2b-256 7a191898d5e63bc164e70d4b6975c88d1cd9bfc9e2950c552a973ccec2f91574

See more details on using hashes here.

File details

Details for the file utemplates-0.26-py3-none-any.whl.

File metadata

  • Download URL: utemplates-0.26-py3-none-any.whl
  • Upload date:
  • Size: 52.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for utemplates-0.26-py3-none-any.whl
Algorithm Hash digest
SHA256 4a2b6430d3bac7b6bb4625f83aa34b571445beb6f7c1da53e23ac00e2307d570
MD5 21d735c36eb52bb7ef765d7b69c44290
BLAKE2b-256 931b5c93ca8a3eb06b77fdfc89b875963b330a957f57a7f4f335550bb68d21a9

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