Muffin-Jinja2 – Support Jinja2 templates for Muffin Framework
Requirements
python >= 3.9
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 = Jinja2(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)
Options
Name |
Default value |
Description |
auto_reload |
False |
Auto reload changed templates |
cache_size |
50 |
Cache templates |
extensions |
None |
Enable Jinja2 Extensions (None | list) |
loader |
FileSystemLoader |
Template loader |
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.init(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
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.8.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| muffin_jinja2-1.8.1.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| muffin_jinja2-1.8.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / muffin_jinja2-1.8.1.tar.gz
| Download URL | muffin_jinja2-1.8.1.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
20dd5c7842aed4c796f061aacfd826fce0b07598a47e5e9ca1cca6c95d208083
|
|
BLAKE2b-256 checksum How to use checksums |
500346606703d026578df334106f72ea5cc32f4bc0244bfa150f783f1d9ea1fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1024-azure
|
Release files / muffin_jinja2-1.8.1-py3-none-any.whl
| Download URL | muffin_jinja2-1.8.1-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7e2fab285c414c303af18527931f85051e95e6ad91006c1b4bb8a25cfccd5e99
|
|
BLAKE2b-256 checksum How to use checksums |
409636ae1f0ad5527f30856a8d177e29116ba1a214330e860b3cfd6c126f59da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1024-azure
|