j2_template
Introduction
This is a template for creating a new Jinja2 template.
实现了一个类似 Ansible 的 template 模块工具,具备以下功能:
- 渲染 Jinja2 模板。
- 支持命令行界面(CLI),可以通过传递模板文件、上下文、输出文件等参数来进行模板渲染。
- 支持
dry-run模式,避免实际输出文件,仅打印渲染结果。 - 支持通过 API 调用,能够将功能集成到其他 Python 项目中。
- 使用
argparse和logging模块来实现命令行参数解析和日志记录,确保工具的易用性和可调试性。
Usage
模版文件:template.j2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<h1>{{ greeting }}</h1>
<p>{{ description }}</p>
</body>
</html>
CLI 模式:
#从文件读取模板和上下文数据:
j2_template --template-file template.j2 --context-file context.json
j2_template --template-file template.j2 --context-file context.json --log-level info
# 从标准输入读取模板内容:
cat template.j2 | j2_template --context '{"title": "Test", "greeting": "Hello!", "description": "This is a description."}' --output result.txt --log-level debug
# 使用环境变量提供上下文数据:
export J2_TEMPLATE_CONTEXT='{"title": "Test", "greeting": "Hello!", "description": "This is a description."}'
j2_template --template-file template.j2 --output result.txt
API 模式:
from j2_template import render_template
# 示例上下文
context = {
'title': 'Test Page',
'greeting': 'Hello, World!',
'description': 'This is a Jinja2 template rendered in Python.'
}
# 渲染模板并输出到控制台
render_template(template_file='test/test_template.html', context=context)
wiki
Metadata
Release files for j2-template 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| j2_template-1.3.0.tar.gz | 6.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| j2_template-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.7 kB
Release files / j2_template-1.3.0.tar.gz
| Download URL | j2_template-1.3.0.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
18fd0530eb7e0a5ce93d3e129b248674e4c24b5eea465fa024838421e0828725
|
|
BLAKE2b-256 checksum How to use checksums |
5aed81b41b5c18cbadece031ca250c2169d5d5a9424a8594e4f5d5cbd5ee68b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.6
|
Release files / j2_template-1.3.0-py3-none-any.whl
| Download URL | j2_template-1.3.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1d217717a475c645b8c8814d901221878ab841d3566173f7c5eda2e2e176bc56
|
|
BLAKE2b-256 checksum How to use checksums |
be27195c343b3a573251436d3c1e62d3f0dfa8e6793efbdacdd7bfac33cb8340
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.6
|