Using Twig
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 PHPvar_dump()
. It is enabled in WordPress debug mode (WP_DEBUG
) or ifICDS_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 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
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.
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 %}
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 current timestamp
now
contains the value of PHP function time()
at the moment of Twig environment initialization.
Get date column from CRM and transform its value
Use format_datetime()
to get value of any date column and transform its value.
{% set record=entities.contact[GUID] %}
{{ record.date_column|format_datetime(dateFormat='short', timeFormat='short', locale=user.locale, timezone=user.timezone) }}
Example: we need to get Birthday column value and to see it as 11/1/22, 12:00 AM
{% set record=entities.contact[9ff7777f-6266-ed11-9562-00224892b4a1] %}
{{ record.birthdate|format_datetime(dateFormat='short', timeFormat='short', locale=user.locale, timezone=user.timezone) }}
You can override the default timezone by explicitly specifying a timezone:
{% set record=entities.contact[9ff7777f-6266-ed11-9562-00224892b4a1] %}
{{ record.birthdate|date("F jS \\a\\t g:ia", "Europe/Paris") }}
You can even define your own pattern using format_datetime() See details:
{% set record=entities.contact[9ff7777f-6266-ed11-9562-00224892b4a1] %}
{{ record.birthdate|format_datetime(pattern="hh 'oclock' a, zzzz") }}
Get lookup value
You can follow the examples below:
{{ entities.contact['ae8bca63-706a-ed11-9561-000d3a227751'].parentcustomerid.Name }}
{{ entities.contact['ae8bca63-706a-ed11-9561-000d3a227751'].parentcustomerid.Id }}
Specify fields to display
When using the expand
parameter, 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.contact['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 argumentspath
-- relative page URLpath_and_query
-- relative page URL with query argumentsquery
-- query arguments, start with?
if not emptyurl
-- 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.