Liquid templates for Python
Project description
Python Liquid2
Liquid templates for Python, with some extra features.
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
- Documentation: https://jg-rp.github.io/python-liquid2/
- Change Log: https://github.com/jg-rp/python-liquid2/blob/main/CHANGES.md
- PyPi: https://pypi.org/project/python-liquid2/
- Source Code: https://github.com/jg-rp/python-liquid2
- Issue Tracker: https://github.com/jg-rp/python-liquid2/issues
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_liquid2-0.3.0.tar.gz.
File metadata
- Download URL: python_liquid2-0.3.0.tar.gz
- Upload date:
- Size: 87.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
410e11afcd5bf2af550396d662e941a5beba0bdd648a7c1368156ecf9fc903ae
|
|
| MD5 |
c54dfbf75473b58dc42a7093b8209a98
|
|
| BLAKE2b-256 |
d6c361db179796ee8443fa7486f4ad88e2e86f73423486ac4866a151f30b5610
|
File details
Details for the file python_liquid2-0.3.0-py3-none-any.whl.
File metadata
- Download URL: python_liquid2-0.3.0-py3-none-any.whl
- Upload date:
- Size: 129.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d877ab3f4183d478b5c5ddd997d7f47a49365c461af99bd37f611f8f2db11737
|
|
| MD5 |
ecf0ed1cba9ca032fbe6c3c0e408fa66
|
|
| BLAKE2b-256 |
186ece6072ec95d4e871b497ced2f628df4b34e9c1b8295c0efcc61fc424a812
|