Support Jinja2 templates for Muffin Framework
Project description
Muffin-Jinja2 – Support Jinja2 templates for Muffin Framework
Requirements
python >= 3.8
Installation
Muffin-Jinja2 should be installed using pip:
pip install muffin-jinja2
Usage
from muffin import Application
from muffin_jinja2 import Plugin as Jinja2
# Create Muffin Application
app = Application('example')
# Initialize the plugin
# As alternative: jinja2 = Jinja2(app, **options)
jinja2 = Jinja2()
jinja2.init(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
Format: Name – Description (default value)
auto_reload – Auto reload changed templates (False)
cache_size – Cache templates (50)
extensions – Enable Jinja2 Extensions (None)
loader – Template loader (FileSystemLoader)
encoding – Default encoding for file loader (utf-8)
template_folders – List of template folders ([‘templates’])
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:
JINJA2_AUTO_RELOAD = True
JINJA2_TEMPLATE_FOLDERS = ['tmpls']
Muffin.Application configuration options are case insensetive
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
Hashes for muffin_jinja2-0.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e08c99fb9e8b9c23e0a25b025e3f30bb71b47acf5ec2fd81f4c7fe029e96d680 |
|
MD5 | a13ed18b4680cc29447c0e3fb1cf73ab |
|
BLAKE2b-256 | 47bcb0f07b2f50501e88e6319a99591c553c7fc97ab1d239d735ae27580d8a15 |