Support Jinja2 templates for Muffin Framework
Project description
Muffin-Jinja2 – Support Jinja2 templates for Muffin Framework
Requirements
python >= 3.7
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.context_processor
def custom_context():
return { 'VAR': 'VALUE' }
# Register a function into global context
@jinja2.register
def sum(a, b):
return a + b
# Register a function with a different name
@jinja2.register('div')
def mod(a, b):
return a // b
# Register a filter
@jinja2.filter
def test(value, a, b=None):
return a if value else b
# Register a filter with a different name
@jinja2.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.
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
File details
Details for the file muffin-jinja2-1.1.0.tar.gz
.
File metadata
- Download URL: muffin-jinja2-1.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49ee7c06b2e81b544cb24723ff867499ea2dce21ded08b4272c2f1ecd9291859 |
|
MD5 | c99622fdbc7477f5027e8ad9b14c610b |
|
BLAKE2b-256 | c475d1380e6bc59685e3982b5eac9d4bc0933a6a4e689a02ee327828fc82b44a |
File details
Details for the file muffin_jinja2-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: muffin_jinja2-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eaf0804382a8b7b962ed09f26152cd3355e42bc3a7a252095e8d70da320acc51 |
|
MD5 | dc9ad1790a5b66f1c0d7709dab8b40fe |
|
BLAKE2b-256 | 7658415f8c2776c85ed65839d52184597bc1d1509c1b9fd6ce16ddfbc9f2de62 |