1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect3 id="zend.view.helpers.initial.currency">
4 <title>Currency Helper</title>
7 Displaying localized currency values is a common task; the
8 <classname>Zend_Currency</classname> view helper is intended to simply this task. See the
9 <link linkend="zend.currency.introduction">Zend_Currency documentation</link> for specifics
10 on this localization feature. In this section, we will focus simply on usage of the view
15 There are several ways to initiate the <emphasis>Currency</emphasis> view helper:
21 Registered, through a previously registered instance in
22 <classname>Zend_Registry</classname>.
28 Afterwards, through the fluent interface.
34 Directly, through instantiating the class.
40 A registered instance of <classname>Zend_Currency</classname> is the preferred usage for
41 this helper. Doing so, you can select the currency to be used prior to adding the adapter to
46 There are several ways to select the desired currency. First, you may simply provide a
47 currency string; alternately, you may specify a locale. The preferred way is to use a
48 locale as this information is automatically detected and selected via the HTTP client
49 headers provided when a user accesses your application, and ensures the currency provided
50 will match their locale.
55 We are speaking of "locales" instead of "languages" because a language may vary based on
56 the geographical region in which it is used. For example, English is spoken in different
57 dialects: British English, American English, etc. As a currency always correlates to a
58 country you must give a fully-qualified locale, which means providing both the language
59 <emphasis>and</emphasis> region. Therefore, we say "locale" instead of "language."
63 <example id="zend.view.helpers.initial.currency.registered">
64 <title>Registered instance</title>
67 To use a registered instance, simply create an instance of
68 <classname>Zend_Currency</classname> and register it within
69 <classname>Zend_Registry</classname> using <classname>Zend_Currency</classname> as its
73 <programlisting language="php"><![CDATA[
74 // our example currency
75 $currency = new Zend_Currency('de_AT');
76 Zend_Registry::set('Zend_Currency', $currency);
79 echo $this->currency(1234.56);
80 // this returns '€ 1.234,56'
85 If you are more familiar with the fluent interface, then you can also create an instance
86 within your view and configure the helper afterwards.
89 <example id="zend.view.helpers.initial.currency.afterwards">
90 <title>Within the view</title>
93 To use the fluent interface, create an instance of <classname>Zend_Currency</classname>,
94 call the helper without a parameter, and call the <methodname>setCurrency()</methodname>
98 <programlisting language="php"><![CDATA[
100 $currency = new Zend_Currency('de_AT');
101 $this->currency()->setCurrency($currency)->currency(1234.56);
102 // this returns '€ 1.234,56'
107 If you are using the helper without <classname>Zend_View</classname> then you can
108 also use it directly.
111 <example id="zend.view.helpers.initial.currency.directly.example-1">
112 <title>Direct usage</title>
114 <programlisting language="php"><![CDATA[
115 // our example currency
116 $currency = new Zend_Currency('de_AT');
118 // initiate the helper
119 $helper = new Zend_View_Helper_Currency($currency);
120 echo $helper->currency(1234.56); // this returns '€ 1.234,56'
125 As already seen, the <methodname>currency()</methodname> method is used to return the
126 currency string. Just call it with the value you want to display as a currency. It also
127 accepts some options which may be used to change the behaviour and output of the helper.
130 <example id="zend.view.helpers.initial.currency.directly.example-2">
131 <title>Direct usage</title>
133 <programlisting language="php"><![CDATA[
134 // our example currency
135 $currency = new Zend_Currency('de_AT');
137 // initiate the helper
138 $helper = new Zend_View_Helper_Currency($currency);
139 echo $helper->currency(1234.56); // this returns '€ 1.234,56'
140 echo $helper->currency(1234.56, array('precision' => 1));
141 // this returns '€ 1.234,6'
146 For details about the available options, search for <classname>Zend_Currency</classname>'s
147 <methodname>toCurrency()</methodname> method.