跳转到主要内容

支持Muffin框架的Jinja2模板

项目描述

Muffin-Jinja2 – 支持为 Muffin 框架提供Jinja2模板

Tests Status PYPI Version

要求

  • python >= 3.9

安装

Muffin-Jinja2 应使用pip安装

pip install muffin-jinja2

使用

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)

选项

名称

默认值

描述

auto_reload

False

自动重新加载更改的模板

cache_size

50

缓存模板

extensions

None

启用Jinja2扩展 (None | list)

loader

FileSystemLoader

模板加载器

encoding

utf-8

文件加载器的默认编码

template_folders

['templates']

模板文件夹列表

您可以在初始化插件时提供选项

jinja2.init(app, template_folders=['src/templates'], auto_reload=True)

或者,在 Muffin.Application 配置中设置它,例如使用 jinja2_ 前缀

JINJA2_AUTO_RELOAD = True

JINJA2_TEMPLATE_FOLDERS = ['tmpls']

Muffin.Application 配置选项不区分大小写

调整

# 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)

错误跟踪器

如果您有任何建议、错误报告或烦恼,请通过https://github.com/klen/muffin-jinja2/issues的问题跟踪器报告。

贡献

Muffin-Jinja2的开发地点为:https://github.com/klen/muffin-jinja2

贡献者

  • klen (Kirill Klenov)

许可证

在MIT许可证下发布。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

muffin_jinja2-1.8.1.tar.gz (5.0 kB 查看散列)

上传时间

构建分发

muffin_jinja2-1.8.1-py3-none-any.whl (5.3 kB 查看散列)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面