A Flask extension for rendering Liquid templates.
Project description
Flask Liquid
A Flask extension for Liquid. Render Liquid templates in your Flask applications.
Install
Install Flask Liquid using pip or your favorite package manager:
pip install flask-liquid
Links
- Documentation (this README): https://github.com/jg-rp/Flask-Liquid/blob/main/README.md
- Documentation for Flask Liquid version 1.x: https://jg-rp.github.io/python-liquid-docs-archive/guides/flask-liquid
- Change Log: https://github.com/jg-rp/flask-liquid/blob/main/CHANGES.md
- PyPi: https://pypi.org/project/flask-liquid/
- Source Code: https://github.com/jg-rp/flask-liquid
- Issue Tracker: https://github.com/jg-rp/flask-liquid/issues
Usage
Flask Liquid provides render_template and render_template_string functions that behave much like the Flask equivalents of the same name. By default Flask Liquid will look for templates in app.template_folder. The same location Flask uses for Jinja templates.
from flask import Flask
from flask_liquid import Liquid
from flask_liquid import render_template
app = Flask(__name__)
liquid = Liquid(app)
@app.route("/hello/")
@app.route("/hello/<name>")
def index(name=None):
return render_template("index.html", name=name)
Set the LIQUID_TEMPLATE_FOLDER configuration value to change the Liquid template folder independently of app.template_folder.
app = Flask(__name__)
app.config.update(
LIQUID_TEMPLATE_FOLDER="/path/to/liquid/templates/",
)
liquid = Liquid(app)
Factories and Blueprints
When using the factory pattern, use Liquid.init_app(app) instead. Any LIQUID_* configuration values stored on the app will override Liquid constructor arguments when init_app is called.
from flask import Flask
from flask_liquid import Liquid
from yourapp.blueprints import some_blueprint
liquid = Liquid()
def create_app(config=None):
app = Flask(__name__)
app.register_blueprint(some_blueprint.bp)
liquid.init_app(app)
return app
Mixing Jinja and Liquid
If you want to use Jinja and Liquid templates side by side, import Liquid render functions using an alias.
from flask import render_template
from flask_liquid import render_template as render_liquid_template
Auto Escape
Unlike a standard liquid.Environment(), Flask Liquid enables HTML auto-escaping by default. You can disable auto-escaping by passing autoescape=False to flask_liquid.Liquid() or setting the LIQUID_AUTOESCAPE configuration value to False.
To render markup from a Liquid snippet inside a Jinja template (or vice versa), mark the string returned by render_liquid_template as safe using Markup, then include it in the Jinja template context. That is assuming you trust values in the Liquid render context or have HTML escaped them already.
from flask import Flask
from flask import Markup
from flask import render_template
from flask_liquid import Liquid
from flask_liquid import render_template as render_liquid_template
app = Flask(__name__)
liquid = Liquid(app)
@app.route("/hello")
def hello():
user_content = render_liquid_template("content.liquid")
return render_template("page.html", content=Markup(user_content))
Flask Standard Context
Flask has some standard context variables that are included in each Jinja template context automatically. Flask Liquid does not include these variables. If you need access to the Flask session or request, for example, you'll need to manually map session or request properties to Liquid context keys.
from flask import Flask
from flask import request
from flask_liquid import Liquid
from flask_liquid import render_template
app = Flask(__name__)
liquid = Liquid(app)
@app.route("/hello/")
@app.route("/hello/<name>")
def index(name=None):
return render_template("index.html", name=name, path=request.path)
Context Processors
When the LIQUID_FLASK_CONTEXT_PROCESSORS configuration value is set to True, Flask context processors will update Liquid template contexts too.
[!NOTE] Remember that Python Liquid uses the Sequence and Mapping interfaces for resolving object properties. See Variables, types and drops.
from flask import Flask
from flask import request
from flask_liquid import Liquid
from flask_liquid import render_template
app = Flask(__name__)
app.config.update(
LIQUID_FLASK_CONTEXT_PROCESSORS=True,
)
liquid = Liquid(app)
@app.context_processor
def extra_context():
return {"request_path": request.path}
@app.route("/hello/")
@app.route("/hello/<name>")
def index(name=None):
return render_template("index.html", name=name)
Async Support
Render templates asynchronously using flask_liquid.render_template_async() and flask_liquid.render_template_string_async().
from flask import Flask
from flask_liquid import Liquid
from flask_liquid import render_template_async
app = Flask(__name__)
liquid = Liquid(app)
@app.route("/render/<name>")
async def render_by_name(name=None):
return await render_template_async(name)
Configuration Values
The following configuration values can be used instead of passing arguments to flask_liquid.Liquid().
| Configuration Value | Liquid Argument | Default |
|---|---|---|
| LIQUID_TAG_START_STRING | tag_start_string | "{%" |
| LIQUID_TAG_END_STRING | tag_end_string | "%}" |
| LIQUID_STATEMENT_START_STRING | statement_start_string | "{{" |
| LIQUID_STATEMENT_END_STRING | statement_end_string | "}}" |
| LIQUID_TEMPLATE_COMMENTS | template_comments | False |
| LIQUID_COMMENT_START_STRING | statement_start_string | "{#" |
| LIQUID_COMMENT_END_STRING | statement_end_string | "#}" |
| LIQUID_TOLERANCE | tolerance | liquid.Mode.STRICT |
| LIQUID_UNDEFINED | undefined | liquid.Undefined |
| LIQUID_STRICT_FILTERS | strict_filters | True |
| LIQUID_TEMPLATE_FOLDER | app.template_folder |
|
| loader | FileSystemLoader(app.template_folder) |
|
| LIQUID_AUTOESCAPE | autoescape | True |
| LIQUID_FLASK_CONTEXT_PROCESSORS | flask_context_processors | False |
| LIQUID_FLASK_SIGNALS | flask_signals | True |
Note that if you specify a loader with the loader argument to flask_liquid.Liquid(), Flask Liquid will use that instead of creating a FileSystemLoader pointing to LIQUID_TEMPLATE_FOLDER.
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
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 flask_liquid-2.0.1.tar.gz.
File metadata
- Download URL: flask_liquid-2.0.1.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33b402ad0da4116a3157deec301a5aaa73e8c42f5b20f25d23424043041ec0d2
|
|
| MD5 |
6b6b94daf9d9852eec3c0ecffee475ac
|
|
| BLAKE2b-256 |
9b745b4386c734f23bd9781a71d11a4c056bbd84261280248afe79cdbd5ba259
|
File details
Details for the file flask_liquid-2.0.1-py3-none-any.whl.
File metadata
- Download URL: flask_liquid-2.0.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
836b21e5b2af9172b33b7b6cd000bc68b046f2b9b20e2fa6c8a540de86c0c079
|
|
| MD5 |
a01a8220274e4cead353780c7bf86a99
|
|
| BLAKE2b-256 |
26972321043e8f9f0773d0945a18ebc42bb5fc5b1b2c2882fe5c5667a7661b67
|