I’ve created a ZF2 view helper PageTitle
(extending ZendViewHelperAbstractHelper
). As the name of the helper suggests , it is responsible for rendering the page title for each action view script.
So within each action view script (not layout) I would call :
echo $this->pageTitle('My Page Title', 'some-icon-class-name');
This would render the required HTML output
<div class="page-title">
<h1><span class="some-icon-class-name"></span> My Page Title</h1>
</div>
The plugin also contains another view helper (button
) within it; its job is to render zero or more form button elements.
My trouble comes when I need to pass these page specific buttons/links. My current solution is to provide an array of ‘buttons’ as a third parameter.
$this->pageTitle('My Page Title', 'some-icon-class-name', array(
'button label 1' => array(
'attributes' => array(
'class' => array('some-class-name', 'another-class'),
'data-foo' => 'bar',
),
)));
(Above is just an example of what I have tried to demonstrate the issue. This is in fact numerous buttons, each with several ‘attributes’ each)
I think the above approach is messy, certainly within the view. It also defeats the purpose of even having a view helper as I will need to provided this config again each time I need to reuse the page header (some pages may use other views as children).
Some solutions I have considered.
Create a page title service factory, per page
Each factory will create a new PageTitle
plugin and inject the ‘button’ config into it. This is then registered ti the view plugin manager under a unique plugin name.
So, as an example, I would change the call in my view to:
echo $this->mySpecificPageTitle(); // no args as pre-constructed
The downside here is that I then need to create allot of factories (just so I can provide very slightly varied arguments).
Provided a ‘service’ name to the plugin
(this is similar to the navigation view helper) then then helper calls this service and it returns the required configuration.
For example:
echo $this->pageTitle('My Page Title', 'some-icon-class-name', 'MyButtonService');
So within the helper:
$buttonConfig = $serviceManager->get('MyButtonService');
This however again means that I would need to create a factory for each ‘MyButtonService’ I need.
I would migrate the plugin code out of the __invoke
method and instead return an instance of your view helper object. From there provide methods for adding buttons which store the data temporarily in an internal data store (array). From there you then just need a render method which uses all the stored data to build the output. This way you don’t have an ever increase list of parameters and can provide easier to call methods which have a specific purpose.
Sample Usage:
echo $this->pageTitle()->setTitle('My Title')->setIcon('some-icon')->addButton('Test Btn')->addButton('Another Button', array('class' => 'some-class'))->render();
All that is required for this is an __invoke
that simple does return $this
and the methods above saving their inputs to internal class parameters. Your existing code can be dropped in the render method and slightly re-factored and you should be set.