Skip to main content

Support Jinja2 templates for Muffin Framework

Project description

Muffin-Jinja2 – Support Jinja2 templates for Muffin Framework

Tests Status PYPI Version

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.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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

muffin-jinja2-1.7.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

muffin_jinja2-1.7.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file muffin-jinja2-1.7.0.tar.gz.

File metadata

  • Download URL: muffin-jinja2-1.7.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for muffin-jinja2-1.7.0.tar.gz
Algorithm Hash digest
SHA256 83f1d58c2037c06c388f8f67c558a6fa56f210a7b01ba1a61910d80409103b23
MD5 22860394e1f992c77a0075d2e0fffa66
BLAKE2b-256 c685362b3f0e7edb719a3edb2f768cf248819e14fab494b9a219ac3053779069

See more details on using hashes here.

File details

Details for the file muffin_jinja2-1.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for muffin_jinja2-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d5cbcde4609b3475608f45c364538e5a65f9a6d5e5ce5b7923317e4289f9143
MD5 cd0ec598df18815cf7203a5a8496ef50
BLAKE2b-256 99d8af676ccbc13a079088d45d1d527aff15da03433dc026a4a5869bc30f85e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page