- Notifications
You must be signed in to change notification settings - Fork11
A Python engine for the Liquid template language.
License
jg-rp/liquid
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Python Liquid is a Python engine forLiquid, the safe, customer-facing template language.
We followShopify/Liquid closely and test against theGolden Liquid test suite.
Table of Contents
Install Python Liquid usingPipenv:
$ pipenv install -u python-liquid
Orpip:
$ pip install python-liquid
Or fromconda-forge:
$ conda install -c conda-forge python-liquid
- 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
- Python Liquid2: A new Python engine for Liquid withextra features.
- LiquidScript: A JavaScript engine for Liquid with a similar high-level API to Python Liquid.
This example renders a template from a string of text with the package-levelrender()
function. The template has just one placeholder variableyou
, which we've given the value"World"
.
fromliquidimportrenderprint(render("Hello, {{ you }}!",you="World"))# Hello, World!
Often you'll want to render the same template several times with different variables. We can parse source text without immediately rendering it using theparse()
function.parse()
returns aBoundTemplate
instance with arender()
method.
fromliquidimportparsetemplate=parse("Hello, {{ you }}!")print(template.render(you="World"))# Hello, World!print(template.render(you="Liquid"))# Hello, Liquid!
Bothparse()
andrender()
are convenience functions that use the default Liquid environment. For all but the simplest cases, you'll want to configure an instance ofEnvironment
, then load and render templates from that.
fromliquidimportCachingFileSystemLoaderfromliquidimportEnvironmentenv=Environment(autoescape=True,loader=CachingFileSystemLoader("./templates"),)
Then, usingenv.parse()
orenv.get_template()
, we can create aBoundTemplate
from a string or read from the file system, respectively.
# ... continued from abovetemplate=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 calledtemplates
with a file calledindex.html
within it, we would expect aTemplateNotFoundError
to be raised when running the example above.
Please seeContributing to Python Liquid.
About
A Python engine for the Liquid template language.