--- name: frappe-impl-jinja description: > Use when building Jinja templates in Frappe: Print Formats, Email Templates, Notification templates, Portal Pages, and custom Jinja methods. Covers template creation workflows, child table handling, conditional sections, styling, multi-language support, and debugging. Prevents N+1 queries, wrong formatting, and Report Print confusion. Keywords: create print format, email template, portal page, pdf, create print format, invoice template, email template, PDF layout, custom print. template, invoice template, jinja methods, notification template, web page template, print format styling. license: MIT compatibility: "Claude Code, Claude.ai Projects, Claude API. Frappe v14-v16." metadata: author: OpenAEC-Foundation version: "2.0" --- # Frappe Jinja Templates Implementation Workflow Step-by-step workflows for building Jinja templates. For syntax reference, see `frappe-syntax-jinja`. **Version**: v14/v15/v16 (V16 Chrome PDF noted) --- ## Master Decision: What Are You Creating? ``` WHAT IS YOUR OUTPUT? │ ├─► Printable PDF (invoice, PO, report)? │ ├─► Standard DocType → Print Format (Jinja) │ └─► Query/Script Report → Report Print Format (JAVASCRIPT!) │ ⚠️ Uses {%= %} NOT {{ }} │ ├─► Automated email with dynamic content? │ └─► Email Template (Jinja, linked to DocType) │ ├─► System notification? │ └─► Notification (Setup > Notification, uses Jinja) │ ├─► Customer-facing web page? │ └─► Portal Page (myapp/www/*.html + *.py) │ └─► Reusable template functions/filters? └─► Custom jenv methods in hooks.py ``` --- ## Workflow 1: Create a Print Format ### Step 1: Create via UI ``` Setup > Printing > Print Format > New - Name: My Invoice Format - DocType: Sales Invoice - Module: Accounts - Standard: No (custom) - Print Format Type: Jinja ``` ### Step 2: Write the Template ```jinja

{{ doc.select_print_heading or _("Invoice") }}

{{ doc.name }} | {{ doc.get_formatted("posting_date") }}

{{ doc.customer_name }}

{% if doc.address_display %}

{{ doc.address_display | safe }}

{% endif %} {% for row in doc.items %} {% endfor %}
# {{ _("Item") }} {{ _("Qty") }} {{ _("Rate") }} {{ _("Amount") }}
{{ row.idx }} {{ row.item_name }} {{ row.qty }} {{ row.get_formatted("rate", doc) }} {{ row.get_formatted("amount", doc) }}
{% for tax in doc.taxes %}

{{ tax.description }}: {{ tax.get_formatted("tax_amount", doc) }}

{% endfor %}

{{ _("Grand Total") }}: {{ doc.get_formatted("grand_total") }}

{% if doc.terms %}
{{ _("Terms and Conditions") }} {{ doc.terms | safe }}
{% endif %} ``` ### Step 3: Test 1. Open a Sales Invoice 2. Menu > Print > Select "My Invoice Format" 3. Verify layout and formatting 4. **ALWAYS** test PDF download — wkhtmltopdf renders differently from browser ### Critical Rules for Print Formats - **ALWAYS** use `doc.get_formatted("field")` for currency, dates, numbers - **ALWAYS** pass parent doc for child rows: `row.get_formatted("rate", doc)` - **ALWAYS** wrap user-facing text with `_("text")` for translation - **ALWAYS** put CSS in a `