Using Currency Helpers in Twig
This bundle provides two Twig filters to display converted price and a function to list available currencies. To learn more read usage details below.
ongr_price
filter
This filter can be applied on price to get converted and formatted price. Price format can be changed by passing following parameters in corresponding order:
- decimals - count of decimal digits. Default is 2.
- to_currency - currency code price will be converted to.
- from_currency - currency code price will be converted from.
- custom_format - custom format for printing price.
Note: Currency codes must follow ISO 4217 standard.
Usage example in Twig:
Formatted price: {{ 1000|ongr_price(2, "USD", "EUR", "%s dollars.") }}
This example converts price from Euros to Dollars and prints it in given format.
ongr_price_list
filter
This filter can be used when you need to list prices in all configured currencies. Common use case is to have price in all currencies and allow user to switch the shown one without reloading the page.
This filter has optional parameters:
- from_currency - currency to convert price from. If missing, default value from config will be used.
- template - custom template to render price list.
For detailed usage example check [Switching Currency on Client Side][2] page.
Custom Price List Template
You can use custom template if you want. When template is rendered prices
array will be passed to it. Here is the list of available keys:
value
- a string representation of the converted price.currency
- currency code (ISO 4217).
Example twig template:
{% for price in prices %}
<span class="currency-{{ price.currency }}">
{{ price.value }}
</span>
{% endfor %}
When your custom template is ready you can use it by passing it directly to the filter.
ongr_currency_list
function
This function prints a list of all configured currencies. If no arguments given, default template will be used. Pass template name to use custom template.
Example, using default template:
{{ ongr_currency_list() }}
Or you can pass custom template on runtime:
{{ ongr_currency_list('AppBundle::currency_list.html.twig') }}
Custom Currency List Template
You can use custom template if you want. When template is rendered currencies
array will be passed to it. Here is the list of available keys:
value
- a string representation of the converted price.code
- currency code.default
- TRUE if this currency is default, FALSE otherwise.
Getting prices with past currency rates
Both ongr_price_list
and ongr_price
support retrieval of the prices with currency rates of the past.
In order to do this, it should be first ensured that the currency rates of the wanted date exist in
elasticsearch, otherwise an exception will be thrown. If the rates exist, however, one additional parameter
can be passed to each of the filters:
Formatted price from 2016 04 13: {{ 1000|ongr_price(2, "USD", "EUR", "%s dollars.", "2016-04-13") }}
Formatted price list from 2016 04 13: {{ 1000|ongr_price_list('', null, "2016-04-13") }}
In the example above the price will be formatted according to 2016 04 13 currency rates. Lastly, it is important to use the date format as shown above.