Currencies within the view
- Posted by thomasw at 22:12:52 // //
- Announcements, Currency
Today the last missing sheep of my new Zend_Currency implementation has been moved to core.
It’s the view helper for currencies which was missing.
Zend_View_Helper_Currency
Together with the new Zend_Currency implementation it simplifies rendering for all using currencies within their application. Let’s take a short look into an working example…
// Bootstrap
$currency = new Zend_Currency(array('value' => 100, 'locale' => 'fr_FR'));
Zend_Registry::set('Zend_Currency', $currency);
We prepared our currency to be used application wide.
Now the view:
// View $this->currency();
This would output “100,00 €” (or something related).
Really simple, isn’t it ?
Of course this would not work when you have multiple currencies which you are working with.
Then you must work with other registry keys and give the wished instance.
// Model
$instance_1 = new Zend_Currency(array('value' => 100, 'locale' => 'fr_FR'));
$instance_2 = new Zend_Currency(array('value' => 500, 'locale' => 'en_US'));
And in the view:
// View $this->currency($instance_1); $this->currency($instance_2);
Please look into the manual for further informations.
There’s nothing more to say than enjoy my new currency implementation for Zend Framework. ;-)
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
Add comment
Fill out the form below to add your own comments

Wednesday, January 13, 2010 - 14:26:49
I’m a bit confused in the usage of this new view helper. For example, it is a common task, that you iterate over a products-array in the view and you want to show the currency values. Is it possible to do the following?
// Zend_Application resource
$currency = new Zend_Currency(array(’locale’ => ‘de_DE’));
Zend_Registry::set(’Zend_Currency’, $currency);
// View
foreach($products as $product)
{
echo $produuct[’name’] . ‘ - ‘ . $this->currency($product[’price’]) . ‘<br />’;
}
Wednesday, January 13, 2010 - 15:39:56
Why are you confused?
Take a look into the manual… the following example is given within there:
// our example currency
$currency = new Zend_Currency(’de_AT’);
// initiate the helper
$helper = new Zend_View_Helper_Currency($currency);
echo $helper->currency(1234.56); // this returns ‘€ 1.234,56′
echo $helper->currency(1234.56, array(’precision’ => 1));
// this returns ‘€ 1.234,6′
As you can see it IS possible and even documented.
Still this is no way I would propose to use as it adds logic to the view itself. You should probably take a look into Zend_Layout and the View Partitials to come to a better solution for you regarding code in the view. :-)
Friday, January 15, 2010 - 15:12:25
I haven’t seen it in the reference guide on the framework website, that’s why I asked for that. This way will help to prevent looping an array twice in controller (or model) for modifying the currency value output and in view for presentation.
This is a great improvement!
Friday, January 15, 2010 - 15:34:48
When you want to use a component from trunk than you must also look into the manual from trunk.
Website points always to the last official release.
Friday, December 17, 2010 - 14:04:38
Thomas
$currency = new Zend_Currency(’pt_BR’);
//$currency = new Zend_Currency(’INR’);
echo $currency->getSymbol();
Indian Currency has got a new symbol i think we need to implement that
Friday, January 7, 2011 - 01:57:48
Currency signs are part of CLDR. Look to unicode (http://cldr.unicode.org) and open an issue at CLDR when you see problems in their data.