Configuring a ZF2 view helper before rendering
The currencyFormat view helper is very easy to use:
1 |
echo $this->currencyFormat($value, 'GBP', 'en_GB'); |
When I was reading the documentation for the currencyFormat view helper, I discovered that you could configure the currency code and locale once rather than in every call:
1 2 |
// Within your view script $this->plugin("currencyformat")->setCurrencyCode("GBP")->setLocale("en_GB"); |
This is obviously useful, but even more useful would be if we could set it once by default and then override if we need to in a specific call.
The easiest way to do this is to use an event listener on the renderer.post View event within a modules’s onBootstrap method, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
namespace Application; use Zend\View\ViewEvent; use Zend\View\Renderer\PhpRenderer; class Module { public function onBootstrap($e) { $events = $e->getApplication()->getEventManager(); $sharedEvents = $events->getSharedManager(); $sharedEvents->attach('Zend\View\View', ViewEvent::EVENT_RENDERER_POST, function($event) { $renderer = $event->getRenderer(); if ($renderer instanceof PhpRenderer) { $renderer->plugin("currencyformat")->setCurrencyCode("GBP")->setLocale('en_GB'); } }); } // ... } |
Now we can simply do call currencyFormat() with the value in our view script:
1 |
echo $this->currencyFormat($value); |
If only the locale is needed, then you can simple use:
Locale::setDefault('en_GB');
http://de2.php.net/manual/de/locale.setdefault.php
Nice one Rob. We've also got a method in our own AbstractActionController called
getViewHelper($helperName)
which basically just does this:return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName);
I think it is useful if you need to "configure" a view helper on a per-action basis (rather than on a global basis like your example).
This may be a rookie question, but where does $sm from 'use($sm)' get defined in your onBootstrap method above?
Mike, It's an error based on where I copied the code from and isn't needed for this specific sample.
Fixed now. Thanks!
I think that better approach is to add initializer to the ViewHelperManager. So if you already have getViewHelperConfig() method in your Module.php, add "initializers" section to its return value:
I use this Method to add the Dojotoolkit to the HeadScript but as test, i noticed that the 'ZendViewView' triggers twice is there any hook that calls only once during the Render event. thanks :)
Nice post. But i like the solution to set up my view helpers in module.config.php, like this:
Matt,
Be aware that if you do it that way, then the ModuleManager can no longer cache the merged config.
Regards,
Rob…
Didn't know that… thanks for the advice.