Skip to main content

A thin wrapper around the Django templating system to render any string as a template. It provides an easy way to render any user inputted string as a regular django template.

Project description

Latest PyPI version Build Status

django-string-renderer

A thin wrapper around the Django templating system to render any string as a template. It provides an easy way to render any user inputted string as a regular django template.

Requirements

This wrapper uses no models and requires no other settings than a django engine in the TEMPLATES setting where the app is used.

This package uses type annotations and mypy to check those annotations.

This package is only tested against Python >= 3.10 and Django >= 4.2.

Installation

Binary Package from PyPi

In order to use this app in a Django project, the simplest way is to install it from PyPi:

pip install django-string-renderer

From the Source Code

In order to modify or test this app you may want to install it from the source code.

Clone the GitHub repository and then run:

pip install -r requirements.txt -r requirements-dev.txt

Usage

Start by adding stringrenderer to your INSTALLED_APPS setting like this:

INSTALLED_APPS = (
    ...,
    'stringrenderer',
)

Then use the StringTemplateRenderer class to build a Django template instance from a string et render the string with the context of your choice:

import stringrenderer

template_string = "Hello {{ recipient.first_name }} {{ recipient.last_name }}!"
renderer = StringTemplateRenderer(template_string)

rendered_content = renderer.render_template(context=dict(recipient=recipient_1), request=request)
rendered_content = renderer.render_template(context=dict(recipient=recipient_2), request=None)

When the first rendering request occurs on a given StringTemplateRenderer instance, a Template object is built from the string passed to __init__(), and cached for the next rendering operations with other contexts.

You may check the template syntax of a string like this:

import stringrenderer

template_string = "Hello {{ recipient.first_name }} {{ recipient.last_name }}!"
is_valid, syntax_error = check_template_syntax(template_string)

Testing

Get the source code from GitHub, follow the installation instructions above, and run the following commands:

python runtests.py
python -m mypy --ignore-missing-imports stringrenderer/

This will run the test suite with the locally installed version of Python and Django.

Projects Using this App

This app is used in the following projects:

  • MyGym Web: a web platform for managing sports clubs. django-string-renderer is used to render the messages and emails addressed to the members.

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

django_string_renderer-0.5.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

django_string_renderer-0.5.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file django_string_renderer-0.5.0.tar.gz.

File metadata

  • Download URL: django_string_renderer-0.5.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for django_string_renderer-0.5.0.tar.gz
Algorithm Hash digest
SHA256 49a13b7ba6d9ea896e4151b3c714ff0956450c58f9d3e59e65de19a3e4e312ae
MD5 105477bcf906823ea1206a815ccbe056
BLAKE2b-256 f2c4c0939fa933ffc1788f106f77aa620de39825c5746f31145179928b26e372

See more details on using hashes here.

File details

Details for the file django_string_renderer-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_string_renderer-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6aa506d25781f7e609e2803907b4c049cd9c7cd10e0baa4c67deefbc02010711
MD5 00ba4913b5d4164172050910f40ddac7
BLAKE2b-256 5d3b1ebde4581ef3429baa95c893a1d0bfe89bf1a2c789c773521d2949505d31

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