Skip to main content

Liquid templates for Python

Project description

Python Liquid2 (unreleased)

Liquid templates for Python, with some extra features.

[!WARNING] This is a preview. Some features and the documentation are incomplete. You can preview the draft migration guide here.


Table of Contents

Install

Install Python Liquid2 from PyPi using pip:

python -m pip install python-liquid2

Or Pipenv:

pipenv install python-liquid2

Or Poetry:

poetry add python-liquid2

Links

Quick start

render()

Here's a very simple example that renders a template from a string of text with the package-level render() function. The template has just one placeholder variable you, which we've given the value "World".

from liquid2 import render

print(render("Hello, {{ you }}!", you="World"))
# Hello, World!

parse()

Often you'll want to render the same template several times with different variables. We can parse source text without immediately rendering it using the parse() function. parse() returns a Template instance with a render() method.

from liquid2 import parse

template = parse("Hello, {{ you }}!")
print(template.render(you="World"))  # Hello, World!
print(template.render(you="Liquid"))  # Hello, Liquid!

Configure

Both parse() and render() are convenience functions that use the default Liquid environment. For all but the simplest cases you'll want to configure an instance of Environment, then load and render templates from that.

from liquid2 import CachingFileSystemLoader
from liquid2 import Environment

env = Environment(
    auto_escape=True,
    loader=CachingFileSystemLoader("./templates"),
)

Then, using env.from_string() or env.get_template(), we can create a Template from a string or read from the file system, respectively.

# ... continued from above
template = env.from_string("Hello, {{ you }}!")
print(template.render(you="World"))  # Hello, World!

# Try to load "./templates/index.html"
another_template = env.get_template("index.html")
data = {"some": {"thing": [1, 2, 3]}}
result = another_template.render(**data)

Unless you happen to have a relative folder called templates with a file called index.html within it, we would expect a TemplateNotFoundError to be raised when running the example above.

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

python_liquid2-0.0.3.tar.gz (79.6 kB view details)

Uploaded Source

Built Distribution

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

python_liquid2-0.0.3-py3-none-any.whl (114.4 kB view details)

Uploaded Python 3

File details

Details for the file python_liquid2-0.0.3.tar.gz.

File metadata

  • Download URL: python_liquid2-0.0.3.tar.gz
  • Upload date:
  • Size: 79.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for python_liquid2-0.0.3.tar.gz
Algorithm Hash digest
SHA256 97fb906f373d4eb0e7a5f1b8adc5e5ad699907831e2352924a50fd544c1d774b
MD5 5d0b9bd86ce6bc5b025c5f823bd66a9c
BLAKE2b-256 2a6fad583ce25818a635f5a6665f8361526576d26952bb5b4f3e19fd8a0a3b47

See more details on using hashes here.

File details

Details for the file python_liquid2-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: python_liquid2-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 114.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for python_liquid2-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a4698c33b32c22499e9ca2ccf1ddc5b1ce223b2f406c23a7de2a9c6ae01a23a2
MD5 fc931694a87d8c7d97057ca028673e86
BLAKE2b-256 ccfdf22187fc243cd5513e0ffeeceb7e6de869da6e1b1f687885108caf87d0ac

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