Skip to content

Instantly share code, notes, and snippets.

@markdrake
Last active August 29, 2015 14:02
Show Gist options
  • Save markdrake/e8291ac9a7c336a0973d to your computer and use it in GitHub Desktop.
Save markdrake/e8291ac9a7c336a0973d to your computer and use it in GitHub Desktop.
How to use a zend framework 2 view helper in any class:
<?php
// This will allow you to use any view helper in any class such as a controller //
// 1. Instantiate the view helper.
// 2. Configure it.
// 3. Use it.
// E.g. Use currency formatter
$currencyFormatter = new CurrencyFormat();
$currencyFormatter->setCurrencyCode('USD');
$currencyFormatter->setLocale('en_US');
$value = 10000;
$formattedValue = $currencyFormatter($value);
var_dump($formattedValue); // Will output $ 1,000 with HTML entities such as &nbsp;
// When this example will be useful?
// In a select element of a form, as the value options are automatically echoed by the formRow view helper
// the currency format needs to be set when the value options are assigned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment