A Python package for generating Jinja2 templates
Project description
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
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
j2_template-1.3.0.tar.gz
(6.8 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 j2_template-1.3.0.tar.gz.
File metadata
- Download URL: j2_template-1.3.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18fd0530eb7e0a5ce93d3e129b248674e4c24b5eea465fa024838421e0828725
|
|
| MD5 |
71830c65cd014055d5a34f99c7340488
|
|
| BLAKE2b-256 |
5aed81b41b5c18cbadece031ca250c2169d5d5a9424a8594e4f5d5cbd5ee68b2
|
File details
Details for the file j2_template-1.3.0-py3-none-any.whl.
File metadata
- Download URL: j2_template-1.3.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d217717a475c645b8c8814d901221878ab841d3566173f7c5eda2e2e176bc56
|
|
| MD5 |
8c5bf0ce7ff1d33578b7bac293abf13f
|
|
| BLAKE2b-256 |
be27195c343b3a573251436d3c1e62d3f0dfa8e6793efbdacdd7bfac33cb8340
|