Skip to content

Commit

Permalink
Merge pull request #110 from AlexaCRM/fix12748
Browse files Browse the repository at this point in the history
add timezone and locale description
  • Loading branch information
georged authored Jan 22, 2025
2 parents 412104d + 74e56af commit 30f58dd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 43 deletions.
2 changes: 1 addition & 1 deletion datapress/using-twig/filters_and_function.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 2
sidebar_position: 3
title: Filters and function
slug: /twig/filters_and_function
tags:
Expand Down
110 changes: 110 additions & 0 deletions datapress/using-twig/separate_columns_in_twig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
sidebar_position: 2
title: Using Twig to display the value of separate columns
slug: /twig/display-separate-column-value
tags:
- Twig
- DataPress
keywords: [DataPress twig, decimal, float, currency, duration, choice]
---

## Displaying Column Values

### Get date column from CRM and transform its value

To display a column value in UTC, use the following Twig code snippet:

```twig
{% set record=entities.contact["11c4c8fa-bf0e-ef11-9f89-0022489310b4"] %}
{{ record.createdon }}
```

To convert a column value to the user's timezone, use the `_local` suffix as shown below:

```twig
{% set record=entities.contact["11c4c8fa-bf0e-ef11-9f89-0022489310b4"] %}
{{ record.createdon_local }}
```

Use `format_datetime()` to get value of any date column and transform its value.

```twig
{% 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

```twig
{% 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:

```twig
{% 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](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#time-zone-pattern-usage):

```twig
{% 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:

```twig
{{ entities.contact['ae8bca63-706a-ed11-9561-000d3a227751'].parentcustomerid.Name }}
{{ entities.contact['ae8bca63-706a-ed11-9561-000d3a227751'].parentcustomerid.Id }}
```

### Choice data type column

Use `formatted_value()` to get the choice data type column value:

```twig
{% set record=entities.contact["dad5909a-973c-ef11-a316-000d3ad268c1"] %}
{{ record | formatted_value("cr1d1_choiceday")}}<br>
```

In this example, the output will be **Monday**.

### Display currency column value

Also use `formatted_value()` to get the currency data type column value:

```twig
{% set record=entities.contact["dad5909a-973c-ef11-a316-000d3ad268c1"] %}
{{ record | formatted_value("cr1d1_currency")}}<br>
```

An example output: **$2.25**.

### Display duration column value

When working with a duration column in your model-driven app, you can display the duration value using the `format_time()` filter and transform the value into minutes for further formatting.

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

[Read more about Date/Time Format Syntax](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax)

### Display decimal and float columns

The format of displaying decimal and float columns depends on the settings of your user in Dataverse. You can follow both examples:

```twig
{% set record=entities.contact["dad5909a-973c-ef11-a316-000d3ad268c1"] %}
{{ record | formatted_value("cr1d1_decimal")}}<br>
{{ record.cr1d1_decimal }}
```

An example output: **50,002.25**.
50 changes: 9 additions & 41 deletions datapress/using-twig/twig_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ The following object members are available:
- `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**](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). 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.
- `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.
Expand All @@ -114,6 +116,12 @@ reads the local database and request parameters to calculate the entity referenc
{% endif %}
```

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

```twig
{{ "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.
Expand Down Expand Up @@ -141,49 +149,9 @@ Use the `entities` object to access any record in your Dataverse instance by its

`now` contains the value of PHP function [`time()`](https://www.php.net/manual/en/function.time.php) 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.

```twig
{% 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

```twig
{% 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:

```twig
{% 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](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#time-zone-pattern-usage):

```twig
{% 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:

```twig
{{ 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.
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.

```twig
{% set contact = entities.contact['ea8157fa-cc32-ef11-8409-000d3a38d58d']|expand('createdby','fullname,Id') %}
Expand Down
2 changes: 1 addition & 1 deletion datapress/using-twig/twig_template.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 4
title: Twig template
slug: /twig/twig_template
tags:
Expand Down

0 comments on commit 30f58dd

Please sign in to comment.