The Html service can be used to generate html dynamically in your application. Lets check it out:

Basic Examples #

$d = Html::div('Super cool');

$s = Html::section(Array('class'=>'row collapse'),$d);

$span = Html::span('nice span!');

$div = Html::div( Array('data-id'=>5,'class'=>'take-action'), 'Click me to do something');

As you can see above, the type of element that you want to build is specified by the method name. If the first parameter passed is an array then those keys and values will be used to build the properties for the element specified by the method name. Other wise it is assumed what is being passed in is the content that should be inside the generated element.

There is no limit to what elements you can build, as this class uses the magic method


to determine what element it should be building.

What about singleton elements (elements that don't have a closing tag)?

Singleton elements such as inputs, textareas, imgs, hrs ect will all be built correctly. The intended element to be built will be crossed check with this array to determine whether or not it is a singleton element and will omit the closing tag accordingly.

Push onto the View Stack #

The Html class does in fact have one hard coded method, the push() method. Calling this before your method call to build your element will cause the resulting html to be pushed onto the current Views html stack.

Html::push()->ul(Html::li('first item'));