View Helpers
View Helpers
View helpers provide a way to programatically generate views and layouts using PHP. A helper can be created that provides one or more functions that returns a displayable object such as a Hazaar\Html\Div, Hazaar\Html\Span or any other object derrived from the Hazaar\Html\Element class.
There are a few view helpers built into Hazaar, such as:
htmlfor easily generating HTML objects programatically, simplifying the interaction between PHP code, variables and the final HTML output.hazaarfor integrating the Hazaar JavaScript client into your views and layouts.warlockfor integrating the Warlock JavaScript client into your views and layouts.
Using a View Helper
Helpers are lazy-loaded. You can reference a helper directly in the view and it will be created on first use.
For example, to use the HTML helper in a view:
<h1>Example</h1>
<?=$this->html->div($this->myString)->class('example-class');?><h1>Example</h1>
{html->div($myString)->class('example-class')}Adding a View Helper with Options
Some helpers need initialisation arguments. In those cases you still need to add the helper explicitly so you can pass options to the constructor.
For example:
<?php
class MyController extends \Hazaar\Controller\Action {
public function index(){
$this->view('index');
$this->view->addHelper('myhelper', ['option' => 'value']);
}
}Info
The HTML view helper uses the Hazaar\Html\Element child classes which support chaining. This is a feature of the HTML classes, not of the view helpers however, it is suggested that view helpers output Hazaar\Html\Element classes where possible.
Custom View Helpers
Custom view helpers allow application developers to implement their own view helpers. This is now the recommended way of implementing re-usable code that directly generates HTML output. Custom view helpers are implemented in the exact same way as the built-in view helpers and therefore have access to all the same functionality.
Warning
It is possible to override built-in view helpers by creating custom view helpers with the same name. This is by design to allow developers that are not happy with a built-in view helper to create their own implementations. However this can also not be what was intended so be careful when selecting custom view helper names.
Creating a custom view helper
By default, custom view helpers are stored in the helpers application directory in a sub-directory names views (in future there may be other types of helpers). If you do no have a helpers/view directory in your application directory you will need to create that before continuing.
Create a new .php file in the helpers/view directory called Example.php (note the first uppercase character as per the standard naming convention). Custom view helpers need to exist in the application namespace so set the namespace to Application\Helper\View and create a new class called Example that extends the Hazaar\View\Helper abstract base class.
<?php
namespace App\Helper\View;
class Example extends \Hazaar\View\Helper {
public function tag($label){
return $this->html->div([$this->html->h1('Example Tag'), $this->html->div($label)]);
}
}The above view helper provides a method named tag() that takes a single argument and returns a DIV object that contains a H1 header and another DIV containing the label. This is a very simplistic example but it is enough for this demonstration.
Using a custom view helper
Using a custom view helper is EXACTLY the same as using a built-in view helper. Just reference it in your view and it will be loaded on first use.
Once the view helper is available you can access the methods it provides as a normal view helper:
<h1>Example</h1>
<?=$this->example->tag('Hello, World');?><h1>Example</h1>
{example->tag('Hello, World')}