erpnext-syntax-jinja
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseERPNext Jinja Templates Syntax Skill
ERPNext Jinja模板语法指南
Correct Jinja syntax for Print Formats, Email Templates, and Portal Pages in ERPNext/Frappe v14/v15/v16.
适用于ERPNext/Frappe v14/v15/v16版本中打印格式、邮件模板和门户页面的标准Jinja语法。
When to Use This Skill
何时使用本指南
USE this skill when:
- Creating or modifying Print Formats
- Developing Email Templates
- Building Portal Pages (www/*.html)
- Adding custom Jinja filters/methods via hooks
DO NOT USE for:
- Report Print Formats (they use JavaScript templating, not Jinja)
- Client Scripts (use erpnext-syntax-clientscripts)
- Server Scripts (use erpnext-syntax-serverscripts)
在以下场景使用本指南:
- 创建或修改打印格式
- 开发邮件模板
- 构建门户页面(www/*.html)
- 通过钩子添加自定义Jinja过滤器/方法
请勿在以下场景使用:
- 报表打印格式(使用JavaScript模板而非Jinja)
- 客户端脚本(请使用erpnext-syntax-clientscripts)
- 服务器脚本(请使用erpnext-syntax-serverscripts)
Context Objects per Template Type
各模板类型对应的上下文对象
Print Formats
打印格式
| Object | Description |
|---|---|
| The document being printed |
| Frappe module with utility methods |
| Translation function |
| 对象 | 描述 |
|---|---|
| 当前要打印的文档 |
| 包含实用工具方法的Frappe模块 |
| 翻译函数 |
Email Templates
邮件模板
| Object | Description |
|---|---|
| The linked document |
| Frappe module (limited) |
| 对象 | 描述 |
|---|---|
| 关联的文档 |
| Frappe模块(功能受限) |
Portal Pages
门户页面
| Object | Description |
|---|---|
| Current user |
| Query parameters |
| Current language |
| Custom context | Via Python controller |
See:for complete details.references/context-objects.md
| 对象 | 描述 |
|---|---|
| 当前用户 |
| 查询参数 |
| 当前语言 |
| Custom context | 通过Python控制器定义 |
参考:查看完整详情。references/context-objects.md
Essential Methods
核心方法
Formatting (ALWAYS use)
格式化(务必使用)
jinja
{# RECOMMENDED for fields in print formats #}
{{ doc.get_formatted("posting_date") }}
{{ doc.get_formatted("grand_total") }}
{# For child table rows - pass parent doc #}
{% for row in doc.items %}
{{ row.get_formatted("rate", doc) }}
{{ row.get_formatted("amount", doc) }}
{% endfor %}
{# General formatting #}
{{ frappe.format(value, {'fieldtype': 'Currency'}) }}
{{ frappe.format_date(doc.posting_date) }}jinja
{# 打印格式中字段的推荐写法 #}
{{ doc.get_formatted("posting_date") }}
{{ doc.get_formatted("grand_total") }}
{# 子表行 - 传入父文档 #}
{% for row in doc.items %}
{{ row.get_formatted("rate", doc) }}
{{ row.get_formatted("amount", doc) }}
{% endfor %}
{# 通用格式化 #}
{{ frappe.format(value, {'fieldtype': 'Currency'}) }}
{{ frappe.format_date(doc.posting_date) }}Document Retrieval
文档检索
jinja
{# Full document #}
{% set customer = frappe.get_doc("Customer", doc.customer) %}
{# Specific field value (more efficient) #}
{% set abbr = frappe.db.get_value("Company", doc.company, "abbr") %}
{# List of records #}
{% set tasks = frappe.get_all('Task',
filters={'status': 'Open'},
fields=['title', 'due_date']) %}jinja
{# 完整文档 #}
{% set customer = frappe.get_doc("Customer", doc.customer) %}
{# 指定字段值(更高效) #}
{% set abbr = frappe.db.get_value("Company", doc.company, "abbr") %}
{# 记录列表 #}
{% set tasks = frappe.get_all('Task',
filters={'status': 'Open'},
fields=['title', 'due_date']) %}Translation (REQUIRED for user-facing strings)
翻译(面向用户的字符串必填)
jinja
<h1>{{ _("Invoice") }}</h1>
<p>{{ _("Total: {0}").format(doc.grand_total) }}</p>See:for all methods.references/methods-reference.md
jinja
<h1>{{ _("Invoice") }}</h1>
<p>{{ _("Total: {0}").format(doc.grand_total) }}</p>参考:查看所有方法。references/methods-reference.md
Control Structures
控制结构
Conditionals
条件判断
jinja
{% if doc.status == "Paid" %}
<span class="label-success">{{ _("Paid") }}</span>
{% elif doc.status == "Overdue" %}
<span class="label-danger">{{ _("Overdue") }}</span>
{% else %}
<span>{{ doc.status }}</span>
{% endif %}jinja
{% if doc.status == "Paid" %}
<span class="label-success">{{ _("Paid") }}</span>
{% elif doc.status == "Overdue" %}
<span class="label-danger">{{ _("Overdue") }}</span>
{% else %}
<span>{{ doc.status }}</span>
{% endif %}Loops
循环
jinja
{% for item in doc.items %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ item.item_name }}</td>
<td>{{ item.get_formatted("amount", doc) }}</td>
</tr>
{% else %}
<tr><td colspan="3">{{ _("No items") }}</td></tr>
{% endfor %}jinja
{% for item in doc.items %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ item.item_name }}</td>
<td>{{ item.get_formatted("amount", doc) }}</td>
</tr>
{% else %}
<tr><td colspan="3">{{ _("No items") }}</td></tr>
{% endfor %}Loop Variables
循环变量
| Variable | Description |
|---|---|
| 1-indexed position |
| True on first |
| True on last |
| Total items |
| 变量 | 描述 |
|---|---|
| 从1开始的索引 |
| 首次循环时为True |
| 最后一次循环时为True |
| 总条目数 |
Variables
变量定义
jinja
{% set total = 0 %}
{% set customer_name = doc.customer_name | default('Unknown') %}jinja
{% set total = 0 %}
{% set customer_name = doc.customer_name | default('Unknown') %}Filters
过滤器
Commonly Used
常用过滤器
| Filter | Example |
|---|---|
| |
| |
| |
| |
| |
See:for all filters.references/filters-reference.md
| 过滤器 | 示例 |
|---|---|
| |
| |
| |
| |
| |
参考:查看所有过滤器。references/filters-reference.md
Print Format Template
打印格式模板
jinja
<style>
.header { background: #f5f5f5; padding: 15px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { border: 1px solid #ddd; padding: 8px; }
.text-right { text-align: right; }
</style>
<div class="header">
<h1>{{ doc.select_print_heading or _("Invoice") }}</h1>
<p>{{ doc.name }}</p>
<p>{{ _("Date") }}: {{ doc.get_formatted("posting_date") }}</p>
</div>
<table class="table">
<thead>
<tr>
<th>{{ _("Item") }}</th>
<th class="text-right">{{ _("Qty") }}</th>
<th class="text-right">{{ _("Amount") }}</th>
</tr>
</thead>
<tbody>
{% for row in doc.items %}
<tr>
<td>{{ row.item_name }}</td>
<td class="text-right">{{ row.qty }}</td>
<td class="text-right">{{ row.get_formatted("amount", doc) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<p><strong>{{ _("Grand Total") }}:</strong> {{ doc.get_formatted("grand_total") }}</p>jinja
<style>
.header { background: #f5f5f5; padding: 15px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { border: 1px solid #ddd; padding: 8px; }
.text-right { text-align: right; }
</style>
<div class="header">
<h1>{{ doc.select_print_heading or _("Invoice") }}</h1>
<p>{{ doc.name }}</p>
<p>{{ _("Date") }}: {{ doc.get_formatted("posting_date") }}</p>
</div>
<table class="table">
<thead>
<tr>
<th>{{ _("Item") }}</th>
<th class="text-right">{{ _("Qty") }}</th>
<th class="text-right">{{ _("Amount") }}</th>
</tr>
</thead>
<tbody>
{% for row in doc.items %}
<tr>
<td>{{ row.item_name }}</td>
<td class="text-right">{{ row.qty }}</td>
<td class="text-right">{{ row.get_formatted("amount", doc) }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<p><strong>{{ _("Grand Total") }}:</strong> {{ doc.get_formatted("grand_total") }}</p>Email Template
邮件模板
jinja
<p>{{ _("Dear") }} {{ doc.customer_name }},</p>
<p>{{ _("Invoice") }} <strong>{{ doc.name }}</strong> {{ _("for") }}
{{ doc.get_formatted("grand_total") }} {{ _("is due.") }}</p>
<p>{{ _("Due Date") }}: {{ frappe.format_date(doc.due_date) }}</p>
{% if doc.items %}
<ul>
{% for item in doc.items %}
<li>{{ item.item_name }} - {{ item.qty }} x {{ item.get_formatted("rate", doc) }}</li>
{% endfor %}
</ul>
{% endif %}
<p>{{ _("Best regards") }},<br>
{{ frappe.db.get_value("Company", doc.company, "company_name") }}</p>jinja
<p>{{ _("Dear") }} {{ doc.customer_name }},</p>
<p>{{ _("Invoice") }} <strong>{{ doc.name }}</strong> {{ _("for") }}
{{ doc.get_formatted("grand_total") }} {{ _("is due.") }}</p>
<p>{{ _("Due Date") }}: {{ frappe.format_date(doc.due_date) }}</p>
{% if doc.items %}
<ul>
{% for item in doc.items %}
<li>{{ item.item_name }} - {{ item.qty }} x {{ item.get_formatted("rate", doc) }}</li>
{% endfor %}
</ul>
{% endif %}
<p>{{ _("Best regards") }},<br>
{{ frappe.db.get_value("Company", doc.company, "company_name") }}</p>Portal Page with Controller
带控制器的门户页面
www/projects/index.html
www/projects/index.html
jinja
{% extends "templates/web.html" %}
{% block title %}{{ _("Projects") }}{% endblock %}
{% block page_content %}
<h1>{{ _("Projects") }}</h1>
{% if frappe.session.user != 'Guest' %}
<p>{{ _("Welcome") }}, {{ frappe.get_fullname() }}</p>
{% endif %}
{% for project in projects %}
<div class="project">
<h3>{{ project.title }}</h3>
<p>{{ project.description | truncate(150) }}</p>
</div>
{% else %}
<p>{{ _("No projects found.") }}</p>
{% endfor %}
{% endblock %}jinja
{% extends "templates/web.html" %}
{% block title %}{{ _("Projects") }}{% endblock %}
{% block page_content %}
<h1>{{ _("Projects") }}</h1>
{% if frappe.session.user != 'Guest' %}
<p>{{ _("Welcome") }}, {{ frappe.get_fullname() }}</p>
{% endif %}
{% for project in projects %}
<div class="project">
<h3>{{ project.title }}</h3>
<p>{{ project.description | truncate(150) }}</p>
</div>
{% else %}
<p>{{ _("No projects found.") }}</p>
{% endfor %}
{% endblock %}www/projects/index.py
www/projects/index.py
python
import frappe
def get_context(context):
context.title = "Projects"
context.projects = frappe.get_all(
"Project",
filters={"is_public": 1},
fields=["name", "title", "description"],
order_by="creation desc"
)
return contextpython
import frappe
def get_context(context):
context.title = "Projects"
context.projects = frappe.get_all(
"Project",
filters={"is_public": 1},
fields=["name", "title", "description"],
order_by="creation desc"
)
return contextCustom Filters/Methods via jenv Hook
通过jenv钩子自定义过滤器/方法
hooks.py
hooks.py
python
jenv = {
"methods": ["myapp.jinja.methods"],
"filters": ["myapp.jinja.filters"]
}python
jenv = {
"methods": ["myapp.jinja.methods"],
"filters": ["myapp.jinja.filters"]
}myapp/jinja/methods.py
myapp/jinja/methods.py
python
import frappe
def get_company_logo(company):
"""Get company logo URL"""
return frappe.db.get_value("Company", company, "company_logo") or ""python
import frappe
def get_company_logo(company):
"""Get company logo URL"""
return frappe.db.get_value("Company", company, "company_logo") or ""Usage
使用示例
jinja
<img src="{{ get_company_logo(doc.company) }}">jinja
<img src="{{ get_company_logo(doc.company) }}">Critical Rules
重要规则
✅ ALWAYS
✅ 务必遵守
- Use for all user-facing strings
_() - Use for currency/date fields
get_formatted() - Use default values:
{{ value | default('') }} - Child table rows:
row.get_formatted("field", doc)
- 所有面向用户的字符串使用包裹
_() - 货币/日期字段使用方法
get_formatted() - 为变量设置默认值:
{{ value | default('') }} - 子表行使用:
row.get_formatted("field", doc)
❌ NEVER
❌ 严禁操作
- Execute queries in loops (N+1 problem)
- Use for user input (XSS risk)
| safe - Heavy calculations in templates (do in Python)
- Jinja syntax in Report Print Formats (they use JS)
- 在循环中执行查询(N+1性能问题)
- 对用户输入使用过滤器(存在XSS风险)
| safe - 在模板中进行复杂计算(应在Python中实现)
- 在报表打印格式中使用Jinja语法(报表使用JS模板)
Report Print Formats (NOT Jinja!)
报表打印格式(非Jinja语法!)
WARNING: Report Print Formats for Query/Script Reports use JavaScript templating.
| Aspect | Jinja (Print Formats) | JS (Report Print Formats) |
|---|---|---|
| Output | | |
| Code | | |
| Language | Python | JavaScript |
html
<!-- JS Template for Reports -->
{% for(var i=0; i<data.length; i++) { %}
<tr><td>{%= data[i].name %}</td></tr>
{% } %}注意:查询/脚本报表的打印格式使用JavaScript模板。
| 维度 | Jinja(普通打印格式) | JS(报表打印格式) |
|---|---|---|
| 输出语法 | | |
| 代码块语法 | | |
| 语言 | Python | JavaScript |
html
<!-- 报表的JS模板示例 -->
{% for(var i=0; i<data.length; i++) { %}
<tr><td>{%= data[i].name %}</td></tr>
{% } %}Version Compatibility
版本兼容性
| Feature | v14 | v15 |
|---|---|---|
| Basic Jinja API | ✅ | ✅ |
| get_formatted() | ✅ | ✅ |
| jenv hook | ✅ | ✅ |
| Portal pages | ✅ | ✅ |
| frappe.utils.format_date with format | ✅ | ✅+ |
| 特性 | v14 | v15 |
|---|---|---|
| 基础Jinja API | ✅ | ✅ |
| get_formatted() | ✅ | ✅ |
| jenv钩子 | ✅ | ✅ |
| 门户页面 | ✅ | ✅ |
| 带格式的frappe.utils.format_date | ✅ | ✅+ |
V16: Chrome PDF Rendering
V16版本:Chrome PDF渲染
Version 16 introduced Chrome-based PDF rendering replacing wkhtmltopdf.
V16版本引入了基于Chrome的PDF渲染,替代了wkhtmltopdf。
Key Differences
主要差异
| Aspect | v14/v15 (wkhtmltopdf) | v16 (Chrome) |
|---|---|---|
| CSS Support | Limited CSS3 | Full modern CSS |
| Flexbox/Grid | Partial | Full support |
| Page breaks | | |
| Fonts | System fonts | Web fonts supported |
| Performance | Faster | Slightly slower |
| 维度 | v14/v15(wkhtmltopdf) | v16(Chrome) |
|---|---|---|
| CSS支持 | 有限的CSS3支持 | 完整的现代CSS支持 |
| Flexbox/Grid | 部分支持 | 完整支持 |
| 分页 | | 推荐使用 |
| 字体 | 仅系统字体 | 支持Web字体 |
| 性能 | 更快 | 略慢 |
CSS Updates for V16
V16版本的CSS更新
css
/* v14/v15 */
.page-break { page-break-before: always; }
/* v16 - both work, but break-* is preferred */
.page-break { break-before: page; }css
/* v14/v15写法 */
.page-break { page-break-before: always; }
/* v16写法 - 两种都兼容,但推荐使用break-* */
.page-break { break-before: page; }Configuration (V16)
配置(V16版本)
python
undefinedpython
undefinedIn site_config.json
在site_config.json中
{
"pdf_engine": "chrome", # or "wkhtmltopdf" for legacy
"chrome_path": "/usr/bin/chromium"
}
undefined{
"pdf_engine": "chrome", # 如需兼容旧版可设置为"wkhtmltopdf"
"chrome_path": "/usr/bin/chromium"
}
undefinedPrint Format Compatibility
打印格式兼容性
Most print formats work unchanged. Update if using:
- Complex CSS layouts (flexbox/grid now fully supported)
- Custom fonts (web fonts now work)
- Advanced page break control
大多数打印格式无需修改即可使用。若使用以下特性则需更新:
- 复杂CSS布局(Flexbox/Grid现在完全支持)
- 自定义字体(Web字体现在可用)
- 高级分页控制
Reference Files
参考文件
| File | Contents |
|---|---|
| Available objects per template type |
| All frappe.* methods |
| Standard and custom filters |
| Complete working examples |
| Mistakes to avoid |
| 文件 | 内容 |
|---|---|
| 各模板类型的可用上下文对象 |
| 所有frappe.*方法 |
| 标准及自定义过滤器 |
| 完整可用示例 |
| 需避免的错误写法 |
See Also
相关内容
- - For jenv configuration in hooks.py
erpnext-syntax-hooks - - For implementation patterns
erpnext-impl-jinja - - For error handling
erpnext-errors-jinja
- - hooks.py中的jenv配置
erpnext-syntax-hooks - - 实现模式
erpnext-impl-jinja - - 错误处理
erpnext-errors-jinja