|currency
Twig Filter
The |currency filter is used to display a currency value.
{{ 100|currency }}
This Twig filter is introduced after installing the Currency plugin (opens new window) available on the October CMS marketplace. You may install it with the following command.
php artisan plugin:install Responsiv.Currency
The filter takes an options argument, as an array that supports various values.
| Option | Description |
|---|---|
| to | To a given currency code |
| from | From a currency code |
| format | Display format. Options: long, short, null. |
| site | Set to true to use currency codes from the site definition. Default: false. |
For example, to convert an amount from USD to AUD.
{{ 1000|currency({ from: 'USD', to: 'AUD' }) }}
If you want to use the base and display currency from the site definition, set the site option to true.
{{ 1000|currency({ site: true }) }}
To display a currency in long or short format.
// $10.00
{{ 1000|currency({ format: '' }) }}
// $10.00 USD
{{ 1000|currency({ format: 'long' }) }}
// $10
{{ 1000|currency({ format: 'short' }) }}
# currency() Function
The currency() Twig function returns the default currency model, giving you access to the currency's formatting properties. This is useful when you need to pass currency settings to JavaScript controls or perform custom formatting.
{% set cur = currency() %}
The returned model provides these properties:
| Property | Type | Example | Description |
|---|---|---|---|
| currency_symbol | string | $ | The currency symbol |
| decimal_scale | int | 2 | Number of decimal places (used to convert base values) |
| decimal_point | string | . | Decimal separator character |
| thousand_separator | string | , | Thousands separator character |
| place_symbol_before | bool | true | Whether the symbol appears before the number |
For example, passing currency settings to a JavaScript control via data attributes:
{% set cur = currency() %}
<div
data-control="price-slider"
data-decimal-scale="{{ cur.decimal_scale }}"
data-currency-symbol="{{ cur.currency_symbol }}"
data-symbol-before="{{ cur.place_symbol_before ? 'true' : 'false' }}"
data-thousand-separator="{{ cur.thousand_separator }}"
>
...
</div>
# PHP Interface
You may interact with the currency functions via the global Currency facade.
For example, to convert an amount from USD to AUD:
Currency::format(1000, ['from' => 'USD', 'to' => 'AUD']);
To display a currency in long or short format
// $10.00 USD
Currency::format(1000, ['format' => 'long']);
// $10
Currency::format(1000, ['format' => 'short']);
# See Also
← |md ajaxHandler() →