Guide 03 · Developer docs

The theme repo and the asset pipeline

Everything a developer changes on a Core dna site lives in its theme repository. This guide covers the repository and its build, printing records in templates, stylesheets and client-side code, and the parts of a site around the main content.

L1–L2
Unit 02 · Records in templates

Dates, numbers and money

Records hand templates raw values: dates already in the site's time zone, prices as decimal strings, totals as money objects. Formatting each in Smarty and in Twig, the site's own date formats, the empty date that Twig turns into today, and money that prints in US English unless you pass a locale.

Who this is for. The theme developer printing a publish date, a price or a cart total. Afterwards you can format each one the same way in either engine, avoid the two silent mistakes (today's date for an empty one, and dollars in the wrong country's style), and keep formatting where it belongs.

The rule: format in the template, keep values raw everywhere else. A value handed to JavaScript, an API or a data attribute stays raw; only what a person reads is formatted.

Dates

A date getter, such as getPublished(), returns a date already converted to the site's time zone. Format it; never convert it again.

Smarty has the platform's format modifier, which uses the site's own formats from System › Global Settings › Internationalization: |format:'date' uses Date format (default d/m/Y), and |format on its own uses Date/Time format (default d/m/Y H:i). Twig has no equivalent modifier, but can read the same setting through _config. For a format of your own, call the date's format() in Smarty and use |date in Twig:

Template code
{$published = $post->getPublished()}
{if $published}
  <time datetime="{$published->format('c')}">{$published|format:'date'}</time>
  <span>{$published->format('j F Y')}</span>
{/if}
{% set published = post.getPublished() %}
{% if published %}
  <time datetime="{{ published|date('c') }}">{{ published|date(_config.get('Globals.i18n.dateFormat')) }}</time>
  <span>{{ published|date('j F Y') }}</span>
{% endif %}
  • Guard the date in Twig. |date turns an empty value into the current date and time, so an unset date prints today instead of nothing.
  • |format takes dates only. It accepts a date, or a date or date-and-time area read as a record, and prints nothing for an empty value. Give it the date itself, not text from getAreaText(): read date areas with the date getters, as in Area input types in templates.
  • Twig on Core dna has no format_date or format_datetime filters: the extension that provides them is not loaded.

Numbers and prices

In Smarty, number_format is allowed as a function and refused as a modifier by the platform's security policy: write {number_format($price, 2)}, not {$price|number_format:2}. In Twig, number_format is a filter. A product's getFinalPrice() returns the price as a decimal string, or nothing when it has no price, so guard it:

Template code
{$price = $product->getFinalPrice()}
{if $price !== null}
  <span class="price">${number_format($price, 2)}</span>
{/if}
{% set price = product.getFinalPrice() %}
{% if price is not null %}
  <span class="price">${{ price|number_format(2) }}</span>
{% endif %}

Money

Cart and checkout totals are also available as money objects, from the getters ending in AsMoney(): getTotalAsMoney(), getItemTotalAsMoney(), getTaxTotalAsMoney(), getDiscountTotalAsMoney(). A money object prints itself with US English conventions, whatever the site's country: Australian dollars come out as A$1,234.50. Pass the locale you want to format():

Template code
{$cart->getTotalAsMoney()->format('en_AU')}
{{ cart.getTotalAsMoney().format('en_AU') }}

With en_AU the same total is $1,234.50; with de_DE a euro amount is 1.234,50 €. Keep the locale in one place, a variable set in the base template, so every price on the site agrees. A currency record offers getSymbol(), getShortName(), getSymbolLocation() (left or right) and getDecimalPlaces() for building a display by hand.

Go deeper

Was this page helpful?

Your answer goes to the docs team.