Format addresses with address templates
An Address Template controls how ERPNext formats an address on documents, print formats, and emails. ERPNext picks the template from the address's Country. If no template exists for that country, it uses the template marked Is Default.
Prerequisites
- System Manager role.
- Basic HTML and Jinja knowledge.
- A sample address with all fields filled, to test the result.
Create or edit a template
- Open the Address Template list. Open the template for the country, or select Add Address Template and select the Country.
- Copy the current template text, so that you can restore it.
- Enter the template in Template.
- Select Is Default only on the one template that is the fallback for all other countries.
- Save.
- Open an address of that country, and print a Quotation or Sales Invoice that uses it. Check line breaks, labels, and missing values.
WARNING
A syntax error affects every document that shows an address of that country. Test before you leave the change in place.
Write the template
Use the Address fieldnames in Jinja. Wrap optional fields in a condition, so that empty fields do not leave blank lines or stray commas:
jinja
{{ address_line1 }}<br>
{% if address_line2 %}{{ address_line2 }}<br>{% endif %}
{{ city }}{% if state %}, {{ state }}{% endif %} {{ pincode }}<br>
{{ country }}| Fieldname | Value |
|---|---|
address_line1 | First address line |
address_line2 | Second address line |
city | City or town |
county | County |
state | State or province |
pincode | Postal code |
country | Country |
phone, fax, email_id | Contact details on the address |
Custom fields on Address are available by their fieldname. Fieldnames are case-sensitive. Use the fieldname, not the label.
Troubleshooting
| Problem | Cause and fix |
|---|---|
| The wrong format appears | The address has the wrong Country, or no template exists for that country. |
| Blank lines or stray commas | Wrap the whole optional line in an if block. |
| A field never appears | The fieldname is wrong, or the address has no value in that field. |
| The template saves, but the print breaks | Check that every HTML tag and Jinja block is closed. |