Utilizing Whitelisted Methods in Jinja Templates

Introduction

Jinja Templates is a key feature in Empress, providing a powerful environment for server-side templating in Python web applications. This guide will delve into the whitelisted methods provided by Empress for use in Jinja templates.

frappe.format

The frappe.format method is used to convert raw values, stored in a database, into a user-friendly format. For instance, it can transform the date ‘2019-09-08’ into ‘08-09-2019’.

Here’s how to use it:

<div>{{ frappe.format('2019-09-08', {'fieldtype': 'Date'}) }}</div>

This will output:

<div>09-08-2019</div>

frappe.format_date

The frappe.format_date method converts a date into a more human-readable, long format.

Usage example:

<div>{{ frappe.format_date('2019-09-08') }}</div>

This will output:

<div>September 8, 2019</div>

frappe.get_url

The frappe.get_url method returns the site url.

Here’s how to use it:

<div>{{ frappe.get_url() }}</div>

This will output:

<div>https://frappe.io</div>

frappe.get_doc

The frappe.get_doc method retrieves a document by its name.

Usage example:

<div>
    {% set doc = frappe.get_doc('Task', 'TASK00002') %}
    {{ doc.title }} - {{ doc.status }}
</div>

This will output:

<div>
    Buy Eggs - Open
</div>

frappe.get_all

The frappe.get_all method returns a list of all records of a DocType. If the fields argument is not given, it only returns the document names.

Here’s how to use it:

<div>
    {% set tasks = frappe.get_all('Task', filters={'status': 'Open'}, fields=['title', 'due_date'], order_by='due_date asc') %}
    {% for task in tasks %}
    <div>
<h3>{{ task.title }}</h3>
<p>Due Date: {{ frappe.format_date(task.due_date) }}</p>
</div>
    {% endfor %}
</div>

This will output:

<div>
<div>
<h3>Redesign Website</h3>
<p>Due Date: September 8, 2019</p>
</div>
<div>
<h3>Add meta tags on websites</h3>
<p>Due Date: September 22, 2019</p>
</div>
</div>

frappe.get_list

Similar to frappe.get_all, frappe.get_list filters records for the current session user based on permissions.

frappe.db.get_value

The frappe.db.get_value method returns a single field value (or a list of values) from a document.

Here’s how to use it:

<div>
<span>
        {% set company_abbreviation = frappe.db.get_value('Company', 'TennisMart', 'abbr') %}
        {{ company_abbreviation }}
    </span>
<div>
        {% set title, description = frappe.db.get_value('Task', 'TASK00002', ['title', 'description']) %}
        <h3>{{ title }}</h3>
<p>{{ description }}</p>
</div>
</div>

This will output:

<div>
<span>TM</span>
</div>

frappe.db.get_single_value

The frappe.db.get_single_value method returns a field value from a Single DocType.

Usage example:

<div>
<span>
        {% set timezone = frappe.db.get_single_value('System Settings', 'time_zone') %}
        {{ timezone }}
    </span>
</div>

This will output:

<div>
<span>
        Asia/Kolkata
    </span>
</div>

frappe.get_system_settings

The frappe.get_system_settings method returns a field value from System Settings.

Here’s how to use it:

<div>
    {% if frappe.get_system_settings('country') == 'India' %}
    <button>Pay via Razorpay</button>
    {% else %}
    <button>Pay via PayPal</button>
    {% endif %}
</div>

This will output:

<div>
<button>Pay via Razorpay</button>
</div>

frappe.get_meta

The frappe.get_meta method returns a doctype meta. It contains information like fields, title_field, image_field, etc.

Usage example:

<div>
<span>
        {% set meta = frappe.get_meta('Task') %}
        Task has {{ meta.fields | len }} fields.
        {% if meta.get_field('status') %}
        It also has a Status field.
        {% endif %}
    </span>
</div>

This will output:

<div>
<span>
        Task has 18 fields. It also has a Status field.
    </span>
</div>

frappe.get_fullname

The frappe.get_fullname method returns the fullname of the user email passed. If user is not passed, it assumes the current logged-in user.

Here’s how to use it:

<div>
<span>The fullname of faris@erpnext.com is {{ frappe.get_fullname('faris@erpnext.com') }}</span>
<span>The current logged in user is {{ frappe.get_fullname() }}</span>
</div>

This will output:

<div>
<span>The fullname of faris@erpnext.com is Faris Ansari</span>
<span>The current logged in user is John Doe</span>
</div>

frappe.render_template

The frappe.render_template method renders a jinja template string or file with context.

Usage example:

<div>
<!-- render a jinja template file -->
    {{ frappe.render_template('templates/includes/footer/footer.html', {}) }}

    <!-- render a jinja template string -->
<p>{{ frappe.render_template('{{ foo }}', {'foo': 'bar'}) }}</p>
</div>

This will output:

<div>
<footer class="web-footer">
<!-- full footer html -->
</footer>
<p>bar</p>
</div>

frappe._

The frappe._ or _(string) method is used for translations.

Here’s how to use it:

<div>
<p>{{ _('This string should get translated') }}</p>
</div>

This will output:

<div>
<p>इस तार का अनुवाद होना चाहिए</p>
</div>

frappe.session.user

This method returns the current session user.

frappe.session.csrf_token

This method returns the current session’s CSRF token.

frappe.form_dict

If the template is being evaluated in a web request, frappe.form_dict is a dict of query parameters, else it is None.

frappe.lang

This represents the current language used by the translation function. It is a two-letter, lowercase code.

By exploring these methods, developers can harness the full power of Jinja Templates in Empress, enhancing the functionality and customization of their business solutions.