Skip to main content

Flask Liquid

A Flask extension for Liquid. Render Liquid templates in your Flask applications.

License Tests
PyPi - Version Python versions


Install

Install Flask Liquid using pip or your favorite package manager:

pip install flask-liquid

Links

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.

Metadata

Release files for Flask-Liquid 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Liquid 2.0.1
File Size Uploaded
flask_liquid-2.0.1.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Liquid 2.0.1
File Interpreter ABI Platform
flask_liquid-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / flask_liquid-2.0.1.tar.gz

Download URL flask_liquid-2.0.1.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
33b402ad0da4116a3157deec301a5aaa73e8c42f5b20f25d23424043041ec0d2
BLAKE2b-256 checksum
How to use checksums
9b745b4386c734f23bd9781a71d11a4c056bbd84261280248afe79cdbd5ba259
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.10

Release files / flask_liquid-2.0.1-py3-none-any.whl

Download URL flask_liquid-2.0.1-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
836b21e5b2af9172b33b7b6cd000bc68b046f2b9b20e2fa6c8a540de86c0c079
BLAKE2b-256 checksum
How to use checksums
26972321043e8f9f0773d0945a18ebc42bb5fc5b1b2c2882fe5c5667a7661b67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.10

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page