Zend_View-Helpers-Currency.xml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.view.helpers.initial.currency">
  4. <title>Currency Helper</title>
  5. <para>
  6. Displaying localized currency values is a common task; the
  7. <classname>Zend_Currency</classname> view helper is intended to simply this task. See the
  8. <link linkend="zend.currency.introduction">Zend_Currency documentation</link> for specifics
  9. on this localization feature. In this section, we will focus simply on usage of the view
  10. helper.
  11. </para>
  12. <para>
  13. There are several ways to initiate the <emphasis>Currency</emphasis> view helper:
  14. </para>
  15. <itemizedlist>
  16. <listitem>
  17. <para>
  18. Registered, through a previously registered instance in
  19. <classname>Zend_Registry</classname>.
  20. </para>
  21. </listitem>
  22. <listitem>
  23. <para>
  24. Afterwards, through the fluent interface.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. Directly, through instantiating the class.
  30. </para>
  31. </listitem>
  32. </itemizedlist>
  33. <para>
  34. A registered instance of <classname>Zend_Currency</classname> is the preferred usage for
  35. this helper. Doing so, you can select the currency to be used prior to adding the adapter to
  36. the registry.
  37. </para>
  38. <para>
  39. There are several ways to select the desired currency. First, you may simply provide a
  40. currency string; alternately, you may specify a locale. The preferred way is to use a
  41. locale as this information is automatically detected and selected via the
  42. <acronym>HTTP</acronym> client headers provided when a user accesses your application, and
  43. ensures the currency provided will match their locale.
  44. </para>
  45. <note>
  46. <para>
  47. We are speaking of "locales" instead of "languages" because a language may vary based on
  48. the geographical region in which it is used. For example, English is spoken in different
  49. dialects: British English, American English, etc. As a currency always correlates to a
  50. country you must give a fully-qualified locale, which means providing both the language
  51. <emphasis>and</emphasis> region. Therefore, we say "locale" instead of "language."
  52. </para>
  53. </note>
  54. <example id="zend.view.helpers.initial.currency.registered">
  55. <title>Registered instance</title>
  56. <para>
  57. To use a registered instance, simply create an instance of
  58. <classname>Zend_Currency</classname> and register it within
  59. <classname>Zend_Registry</classname> using <classname>Zend_Currency</classname> as its
  60. key.
  61. </para>
  62. <programlisting language="php"><![CDATA[
  63. // our example currency
  64. $currency = new Zend_Currency('de_AT');
  65. Zend_Registry::set('Zend_Currency', $currency);
  66. // within your view
  67. echo $this->currency(1234.56);
  68. // this returns '€ 1.234,56'
  69. ]]></programlisting>
  70. </example>
  71. <para>
  72. If you are more familiar with the fluent interface, then you can also create an instance
  73. within your view and configure the helper afterwards.
  74. </para>
  75. <example id="zend.view.helpers.initial.currency.afterwards">
  76. <title>Within the view</title>
  77. <para>
  78. To use the fluent interface, create an instance of <classname>Zend_Currency</classname>,
  79. call the helper without a parameter, and call the <methodname>setCurrency()</methodname>
  80. method.
  81. </para>
  82. <programlisting language="php"><![CDATA[
  83. // within your view
  84. $currency = new Zend_Currency('de_AT');
  85. $this->currency()->setCurrency($currency)->currency(1234.56);
  86. // this returns '€ 1.234,56'
  87. ]]></programlisting>
  88. </example>
  89. <para>
  90. If you are using the helper without <classname>Zend_View</classname> then you can
  91. also use it directly.
  92. </para>
  93. <example id="zend.view.helpers.initial.currency.directly.example-1">
  94. <title>Direct usage</title>
  95. <programlisting language="php"><![CDATA[
  96. // our example currency
  97. $currency = new Zend_Currency('de_AT');
  98. // initiate the helper
  99. $helper = new Zend_View_Helper_Currency($currency);
  100. echo $helper->currency(1234.56); // this returns '€ 1.234,56'
  101. ]]></programlisting>
  102. </example>
  103. <para>
  104. As already seen, the <methodname>currency()</methodname> method is used to return the
  105. currency string. Just call it with the value you want to display as a currency. It also
  106. accepts some options which may be used to change the behaviour and output of the helper.
  107. </para>
  108. <example id="zend.view.helpers.initial.currency.directly.example-2">
  109. <title>Direct usage</title>
  110. <programlisting language="php"><![CDATA[
  111. // our example currency
  112. $currency = new Zend_Currency('de_AT');
  113. // initiate the helper
  114. $helper = new Zend_View_Helper_Currency($currency);
  115. echo $helper->currency(1234.56); // this returns '€ 1.234,56'
  116. echo $helper->currency(1234.56, array('precision' => 1));
  117. // this returns '€ 1.234,6'
  118. ]]></programlisting>
  119. </example>
  120. <para>
  121. For details about the available options, search for <classname>Zend_Currency</classname>'s
  122. <methodname>toCurrency()</methodname> method.
  123. </para>
  124. </sect3>
  125. <!--
  126. vim:se ts=4 sw=4 et:
  127. -->