Muffin-Jinja2
Muffin-Jinja2 – Support Jinja2 templates for Muffin Framework
Requirements
python >= 3.11
Installation
Muffin-Jinja2 should be installed using pip:
pip install muffin-jinja2
Usage
import muffin
import muffin_jinja2
# Create Muffin Application
app = muffin.Application('example')
# Initialize the plugin
# As alternative: jinja2 = muffin_jinja2.Plugin(app, **options)
jinja2 = muffin_jinja2.Plugin()
jinja2.setup(app, template_folders=['src/templates'])
# Use it inside your handlers
@app.route('/')
async def index(request):
context = {'var': 42}
return await jinja2.render('index.html', **context)
With src/templates/index.html looking like:
<p>{{ var }}</p>
Options
Name |
Default value |
Description |
auto_reload |
False |
Auto reload changed templates |
cache_size |
400 |
Cache templates |
extensions |
() |
Enable Jinja2 Extensions (list | tuple) |
loader |
None |
Template loader, a FileSystemLoader for template_folders is created by default |
encoding |
utf-8 |
Default encoding for file loader |
template_folders |
['templates'] |
List of template folders |
You are able to provide the options when you are initiliazing the plugin:
jinja2.setup(app, template_folders=['src/templates'], auto_reload=True)
Or setup it inside Muffin.Application config using the jinja2_ prefix for example:
JINJA2_AUTO_RELOAD = True
JINJA2_TEMPLATE_FOLDERS = ['tmpls']
Muffin.Application configuration options are case insensitive
Public API
The plugin instance provides the following members.
- env
The jinja2.Environment. It is created by setup(); any access before setup raises muffin.plugins.PluginNotInstalledError.
- loader
The active template loader – a jinja2.FileSystemLoader built from template_folders unless the loader option is given.
- Error
Alias for jinja2.TemplateError.
- add_global(func), add_filter(func)
Decorators to register a function in the environment globals or filters. Both accept a name as first argument to register under another name:
@jinja2.add_global('div') def mod(a, b): return a // b- add_context(func)
Decorator to register a context processor. The function may be a coroutine and its result is merged into the context of every rendering.
- async get_context(**context)
Build the rendering context: environment globals, then all registered context processors, then the given keyword arguments.
- async render(path, **context)
Render a template. path is either a template name or a jinja2.Template object. Returns the rendered string.
- receivers
A plain list of callbacks, empty by default. Each callback is called as reciever(path, ctx) right before a template is rendered, where path is a template name or a jinja2.Template object and ctx is the context dict – mutate it to add values. Callbacks are synchronous and their return value is ignored. There is no decorator for them, add them manually:
def track(path, ctx): ctx['request_path'] = path jinja2.receivers.append(track)
Inside templates the application is available as app and the debug global dumps the current context ({{ debug() }}) or a given value ({{ debug(value) }}).
Deprecated
register, filter and context_processor are the old names of add_global, add_filter and add_context. They still work but emit DeprecationWarning.
Tunning
# Register custom context processor
# could be a function/coroutine
@jinja2.add_context
def custom_context():
return { 'VAR': 'VALUE' }
# Register a function into global context
@jinja2.add_global
def sum(a, b):
return a + b
# Register a function with a different name
@jinja2.add_global('div')
def mod(a, b):
return a // b
# Register a filter
@jinja2.add_filter
def test(value, a, b=None):
return a if value else b
# Register a filter with a different name
@jinja2.add_filter('bool')
def boolean(value):
return bool(value)
@app.route('/')
async def index(request):
""" Check for user is admin. """
local_context = {'key': 'value'}
return await jinja2.render('index.html', **local_context)
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/muffin-jinja2/issues
Contributing
Development of Muffin-Jinja2 happens at: https://github.com/klen/muffin-jinja2
Contributors
klen (Kirill Klenov)
License
Licensed under a MIT license.
Metadata
Release files for muffin-jinja2 1.9.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| muffin_jinja2-1.9.0.tar.gz | 6.6 kB | Without provenance | Details for muffin_jinja2-1.9.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| muffin_jinja2-1.9.0-py3-none-any.whl | Python 3 | none | any | Details for muffin_jinja2-1.9.0-py3-none-any.whl |
Total release size: 13.2 kB
Release files / muffin_jinja2-1.9.0.tar.gz
| Download URL | muffin_jinja2-1.9.0.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2cbdc02aa3fbae1e9539afd2a1ac566712ba3d7ed25a739a90e9b398b654f507
|
|
BLAKE2b-256 checksum How to use checksums |
1ba22a90edaa7752f0f07d2837bbed848e863b1dad64346602bf2dd18513c9cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / muffin_jinja2-1.9.0-py3-none-any.whl
| Download URL | muffin_jinja2-1.9.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d733a52ed66c75673d04634cf82e40f77317a02a258a0878cbf4165d9def58ba
|
|
BLAKE2b-256 checksum How to use checksums |
c6ffb5a37120169d2c41bb27a5724a0c511e29bd8069c9f3ee488b227feb77aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for muffin-jinja2 1.9.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.