Skip to main content

Using Twig

note

The plugin previously known as Dataverse Integration has been renamed to DataPress. This change reflects our commitment to enhancing user experience and aligning with our evolving product vision. All references to Dataverse Integration in the documentation, user interface will be updated to DataPress.

Use Twig templates to create custom layouts. Access your Dataverse data and metadata in Twig to share your data with your users.

Introduction

DataPress (Dataverse Integration) employs Twig to make most data-rich layout jobs effortless.

On this page you can find information about custom Twig features that Integration DataPress (Dataverse Integration) brings. Please refer to Twig documentation to learn about its templating capabilities.

Runtime settings

Twig in DataPress (Dataverse Integration) supports debug mode and template caching.

  • Debug mode allows using dump() to print information about Twig objects using PHP var_dump(). It is enabled in WordPress debug mode (WP_DEBUG) or if ICDS_TWIG_DEBUG is true.
  • Template caching enhances page rendering performance. Enabled if ICDS_TWIG_CACHE is true.

Supporting Mobile-Detect

The MobileDetect class contains various functions for detecting mobile devices and browsers. Read more

isMobile: {% if isMobile %} Yes! {% else %} No! {% endif %}<br/>

isChrome: {% if isChrome %} Yes! {% else %} No! {% endif %}<br/>

isFirefox: {% if isFirefox %} Yes! {% else %} No! {% endif %}<br/>
Intellisense

Intellisense for working with the mobile detection methods is available in the Dataverse Twig block after typing {{.

Global objects

DataPress (Dataverse Integration) makes several new global objects available in the Twig environment.

Access the current bound record

info

Premium feature! This feature is available in the premium extension.

Use the binding object to access table binding on the current page. See table binding.

binding includes several properties:

  • is_bound -- (boolean) whether current page supports table binding.
  • reference -- (EntityReference) reference to the bound record.
  • record -- (Entity) bound record object.

Notice that binding.record is more expensive performance-wise -- it retrieves data from Dataverse. binding.reference only reads the local database and request parameters to calculate the entity reference.

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

If ICDS_COMPATIBLE_BINDING flag is set to true you can use the currentrecord variable that refers to the binding.record. This is intended mostly for backward compatibility with previous versions of the plugin and should not be used in general.

{% if binding.is_bound %}
{{ currentrecord["fullname"] }} <{{ currentrecord["emailaddress1"] }}>
{% endif %}

Access the current user record

Use the user object to check whether the current user is bound, and to access the bound Dataverse record values. See user binding.

The following object members are available:

  • is_bound -- (boolean) whether the current user is bound.
  • reference -- (EntityReference) reference to the bound record.
  • record -- (Entity) bound record object.
  • wp_user -- (WP_User) information about the current WordPress user.
  • timezone-- Returns the timezone for the current user. The timezone should not be null and typically returns as a string. Example output: America/New_York, UTC. The format "Asia/Tokyo" is known as an IANA time zone name. The exact format depends on how the timezone is stored and managed in your WordPress setup. If you need to convert or manipulate this value further, you can use additional Twig filters or functions as needed. If user timezone is not specified, it returns the actual site zone.
  • locale -- Return locale for the current user. Example output: en_GB

Notice that user.record is more expensive performance-wise -- it retrieves data from Dataverse. user.reference only reads the local database and request parameters to calculate the entity reference.

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

An example of date display with explicit time zone and locale:

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

Access any record in your Dataverse instance

Use the entities object to access any record in your Dataverse instance by its table logical name and GUID. All record fields are available at once.

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

Access the list of tables in your Dataverse instance

entities_list contains a map of all tables in your Dataverse instance. It maps logical names to display names.

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 %}

Get site timezone and locale

Use the site object to get the site's locale or timezone:

  • timezone-- Returns the timezone for the site. The format "Asia/Tokyo" is known as an IANA time zone name. The exact format depends on how the timezone is stored and managed in your WordPress setup (Settings -> General).
  • locale -- Returns the locale for the site.

Get current timestamp

now contains the value of PHP function time() at the moment of Twig environment initialization.

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') %}

Get current request information

Object request contains information about the current request. It has the following members:

  • params -- merged map of cookies, POST form values and GET query arguments
  • path -- relative page URL
  • path_and_query -- relative page URL with query arguments
  • query -- query arguments, start with ? if not empty
  • url -- full request URL

Global object params is the alias of request.params.

Check if the website is connected to Dataverse

crm.connected tells whether the website is configured to make connections to Dataverse.