I've created a ZF2 view helper PageTitle
(extending Zend\View\Helper\AbstractHelper
). 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.