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

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


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-0.7.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

muffin_jinja2-0.7.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: muffin-jinja2-0.7.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for muffin-jinja2-0.7.2.tar.gz
Algorithm Hash digest
SHA256 e459e09a5b249dabc8df6b04f1f124d9e062e2789eaed13d23b47e77e69fdb02
MD5 121b9d5062ef71b1d68bb2a82a04d2f4
BLAKE2b-256 902fd341ae6044e1a718880248711ac70d3fe1f746435b761c2f565b6813ef78

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: muffin_jinja2-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for muffin_jinja2-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cac5c620c8380f4673cbd285a0ddb13fd4b780618081b3b99f55439ddbc14106
MD5 bfdc10f210fdcb02d4f740add62c26a2
BLAKE2b-256 32e084a48fdc0b7d8e9600dd5b3e2576047a8ff17e6317b39cad92c7d875932c

See more details on using hashes here.

Provenance

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