Zend_View-Helpers-Currency.xml 5.3 KB

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