A Python engine for the Liquid template language.
Project description
Python Liquid
Python Liquid is a Python engine for Liquid, the safe, customer-facing template language.
We follow Shopify/Liquid closely and test against the Golden Liquid test suite.
Table of Contents
Install
Install Python Liquid using Pipenv:
$ pipenv install -u python-liquid
Or pip:
$ pip install python-liquid
Or from conda-forge:
$ conda install -c conda-forge python-liquid
Links
- Documentation: https://jg-rp.github.io/liquid/
- Documentation for Python Liquid version 1.x: https://jg-rp.github.io/python-liquid-docs-archive/
- Change Log: https://github.com/jg-rp/liquid/blob/main/CHANGES.md
- PyPi: https://pypi.org/project/python-liquid/
- Source Code: https://github.com/jg-rp/liquid
- Issue Tracker: https://github.com/jg-rp/liquid/issues
Related Projects
- Python Liquid2: A new Python engine for Liquid with extra features.
- LiquidScript: A JavaScript engine for Liquid with a similar high-level API to Python Liquid.
Quick Start
render()
This example 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 liquid 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 BoundTemplate
instance with a render()
method.
from liquid 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 liquid import CachingFileSystemLoader
from liquid import Environment
env = Environment(
autoescape=True,
loader=CachingFileSystemLoader("./templates"),
)
Then, using env.parse()
or env.get_template()
, we can create a BoundTemplate
from a string or read from the file system, respectively.
# ... continued from above
template = env.parse("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.
Contributing
Please see Contributing to Python Liquid.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python_liquid-2.0.0.tar.gz
.
File metadata
- Download URL: python_liquid-2.0.0.tar.gz
- Upload date:
- Size: 119.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e14aecffda7a194da7be3a57b66b40b701d8b3b8347fd586e8bb851af8b6244 |
|
MD5 | bd3d2530a6ec24a6a6e8dc9598f496c2 |
|
BLAKE2b-256 | fef50c90efc1931f3d436230b7321e5602a2bbbaf4699f35828cca9e3160878c |
File details
Details for the file python_liquid-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: python_liquid-2.0.0-py3-none-any.whl
- Upload date:
- Size: 197.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1007862a81fcaab8185346850ef6d47f3babbf9ba9da3c55818527c64ae261fb |
|
MD5 | a97626ded9606c3cdbbcf60d548f6e22 |
|
BLAKE2b-256 | 5d1f7face1f506c67f7e97971ae84cb8a531bb7f436a93798edc2578ad30d107 |