让jinja2自带各类好用的filter
Project description
x_jinja2
自带各类好用过滤器的 jinja2 渲染器。
安装
你可以通过 pip 安装 x_jinja2包:
pip install x_jinja2
简单用法
from x_jinja2 import XEnvironment
env = XEnvironment()
output = env.from_string("{{ data.foo | normalize_csv }}").render(
data={
"foo": "bar",
},
)
print(output)
详细用法
导入并使用 XEnvironment
x_jinja2 提供了一个扩展的 XEnvironment 类,该类继承自 jinja2.Environment 并添加了一些有用的过滤器。以下是如何使用 XEnvironment 的示例:
from x_jinja2 import XEnvironment
# 创建一个 XEnvironment 实例
env = XEnvironment(
loader=FileSystemLoader('templates'), # 模板文件加载器
autoescape=select_autoescape(['html', 'xml']) # 自动转义
)
# 加载模板
template = env.get_template('example.html')
# 渲染模板
rendered = template.render(name="World")
print(rendered)
使用过滤器
XEnvironment 包含了一些内置的过滤器,例如 normalize_csv。以下是如何在模板中使用这些过滤器的示例:
<!-- example.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<p>{{ name | normalize_csv }}</p>
</body>
</html>
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
x_jinja2-0.1.1.tar.gz
(3.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file x_jinja2-0.1.1.tar.gz.
File metadata
- Download URL: x_jinja2-0.1.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e50d310e077788f17aa4fd38952e3e71128ba8c11674e7d3db449e9b98524208
|
|
| MD5 |
4941e8f6376524096c987265c963726e
|
|
| BLAKE2b-256 |
9e3bf289907b226251ade5287dd782a72cd4beac511c21dfc0d02d70c50c5a14
|
File details
Details for the file x_jinja2-0.1.1-py3-none-any.whl.
File metadata
- Download URL: x_jinja2-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cd85316a9aa7c8de40694387b707b1e03966b4a3fc4a2ea1420f7c92a213500
|
|
| MD5 |
d4ccdaa926f995de5b1681eba80bc2c3
|
|
| BLAKE2b-256 |
74feccb4f0bbb0d7a470f20532b422783014551b67fb8c90021aa0776142fe3a
|