Skip to main content

A Django template backend for Liquid.

Project description

A Django template backend for Liquid. Render Liquid templates in your Django apps.

Version Licence Python versions

Installing

Install and update using pip:

$ python -m pip install -U django-liquid

Quick Start

If you’re already familiar with configuring Django for Jinja2, Liquid works in almost exactly the same way. Just with less options. Simply set BACKEND to django_liquid.liquid.Liquid.

Add configuration for the Liquid template engine to your Django project’s settings.py file. Starting with the default TEMPLATE configuration after running django-admin startproject mysite, basic Liquid configuration would look like this.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
    {
        'BACKEND': 'django_liquid.liquid.Liquid',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {},
    },
]

When APP_DIRS is True, Liquid engines look for templates in the liquid subdirectory of installed applications.

OPTIONS are passed to the liquid.Environment constructor. The default Environment is configured as follows.

  • autoescape: True

  • loader: a FileSystemLoader configured for DIRS and APP_DIRS

  • undefined: DebugUndefined if settings.DEBUG else Undefined

Render Liquid templates from your app views just like any other Django template backend.

from django.shortcuts import render

def index(request):
    context = {"greeting": "hello"}
    return render(request, 'myapp/index.liquid', context)

If you’ve got multiple template engines configured, like in the example above, Django will use the first engine and template it finds matching the given template name. You can force Django to use a specific template engine with the using argument.

from django.shortcuts import render

def index(request):
    context = {"greeting": "hello"}
    return render(request, 'myapp/index.html', context, using='liquid')

Environment Factory

You can configure your liquid.Environment with additional tags or filters by setting the environment template backend option to the name of an Environment factory function. Lets say you want to register the json filter from python-liquid-extra. If the following is saved as myproject/liquid.py

from liquid import Environment
from liquid_extra import filters

def environment(**options):
    env = Environment(**options)
    env.add_filter("json", filters.JSON())
    # Register more filters or tags here.
    return env

Then tell the django template backend to use your environment factory function like this.

# settings.py
TEMPLATES = [
    {
        'BACKEND': 'django_liquid.liquid.Liquid',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
          'environment': 'myproject.liquid.environment'
        },
    },
]

Contributing

  • Install development dependencies with Pipenv

  • Python Liquid fully embraces type hints and static type checking. I like to use the Pylance extension for Visual Studio Code, which includes Pyright for static type checking.

  • Format code using black.

  • Write tests using unittest.TestCase.

  • Run tests with make test.

  • Check test coverage with make coverage and open htmlcov/index.html in your browser.

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-liquid-0.1.1.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

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

django_liquid-0.1.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file django-liquid-0.1.1.tar.gz.

File metadata

  • Download URL: django-liquid-0.1.1.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for django-liquid-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5801fbf40c7e9f84e72c0689bd96c818f72741708538a086be7c039882e7f6df
MD5 dcf79928abb4adea30d503aa1f5d2619
BLAKE2b-256 f43e173dddb5b95b0cdf2fcee031ddec11364d997c7f6c36f21cd2c785b23966

See more details on using hashes here.

File details

Details for the file django_liquid-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_liquid-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for django_liquid-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a99b129b0182c1f2b53fdad84213e7d1e3d1bde0093a401e1a78bd1a875d5f97
MD5 8a3784126b094bf69db310360bdd84d6
BLAKE2b-256 64c4007676b65999b090d5343d35afb4f2f30ea48016e2900a3c49e14063b41d

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