Skip to main content

Twig Examples

This page provides practical Twig snippets for displaying data, formatting values, and building custom forms in DataPress.


Render a Page-Bound Record

{% if binding.is_bound %}
{% set contact = binding.record %}
{{ contact["fullname"] }} <{{ contact["emailaddress1"] }}>
{% endif %}

Display Current User Information


{% if user.is_bound %}
{{ user.record["fullname"] }}
{% endif %}

Access your Dataverse Integration organization metadata

metadata object allows accessing metadata of your Dataverse instance. It follows the interface of EntityMetadata in XRM SDK. See Microsoft reference docs.

{% set options = metadata["contact"].Attributes["gendercode"].OptionSet.Options %}
{% for option in options %}
<li>{{option.Value}} - {{option.Label.UserLocalizedLabel.Label}}</li>
{% endfor %}

Format Date Using User Locale & Timezone

{{ "now" | format_datetime('short', 'short', locale=user.locale, timezone=user.timezone) }}

Access Any Dataverse Record

{{ entities.contact["00000000-0000-0000-0000-000000000000"]["fullname"] }}

Using Twig to display the value of separate columns

Working with Date Columns

Standard formatting

{{ record.birthdate | format_datetime(
dateFormat='short',
timeFormat='short',
locale=user.locale,
timezone=user.timezone
) }}

Convert timezone

{{ record.birthdate | date("F jS \\a\\t g:ia", "Europe/Paris") }}

Custom datetime pattern

{{ record.birthdate | format_datetime(pattern="hh 'oclock' a, zzzz") }}

Calculate real GMT offset

{{ "2024-08-11T17:39:00+03" | timezone_offset("Australia/Sydney") }}

Lookup Fields

{{ record.parentcustomerid.Name }}
{{ record.parentcustomerid.Id }}

Choice Fields

{{ record | formatted_value("cr1d1_choiceday") }}

Currency Fields

{{ record | formatted_value("cr1d1_currency") }}

Duration Fields

Convert CRM duration (minutes) → readable format:

{% set record=entities.contact["dad5909a-973c-ef11-a316-000d3ad268c1"] %}
{{ record.cr8d6_duration*60 | format_time(pattern: 'mm min. ss sec.') }}

Specify fields to display

When using the expand filter, you can specify which fields to display. If you don’t specify any fields, all of them will be selected. Fields are specified as an array or a comma-delimited string.

{%  set contact = entities.contact['ea8157fa-cc32-ef11-8409-000d3a38d58d']|expand('createdby','fullname,Id') %}

{% set contact = entities.contact['ea8157fa-cc32-ef11-8409-000d3a38d58d']|expand('createdby',['fullname']) %}

{% set contact = entities.contac​t['ea8157fa-cc32-ef11-8409-000d3a38d58d']|expand('createdby') %}