Adding additional tag attributes with additionalAttributes

Most of the ViewHelpers can be split into two types: One group of ViewHelpers is somewhat functional oriented. For example, that applies to the format Viewhelpers, which can format data or currencies. The other group is mostly output-oriented because these Viewhelpers output mostly an HTML tag. Samples of this are the form Viewhelpers. All of them start with f:form. Every form Viewhelper generates a HTML tag, like e.g. <form> or <input>.

A Fluid Viewhelper supports most attributes that are also available in HTML. There are, for example, the attributes class and id, which exists in all tag-based Viewhelper. You find a listing of all universal properties in appendix C.

However, sometimes attributes are needed that are not provided by the Viewhelper - maybe a special JavaScript event handler or proprietary attributes (which are used by JavaScript frameworks like Dojo, for example). To output them as well without changing the Viewhelper, the attribute additionalAttributes is available. This is an associative array by which additional tag attributes can be defined, like the following example shows:

<f:form.textbox id="myTextbox" additionalAttributes="{onclick: 'alert(\'Hello World\')'}" />

Another common use case is the data-attribute, which can be used in JavaScript functions:

<f:form.textbox additionalAttributes="{data-anything: 'some info'}" />

The HTML tag generated by the Viewhelper now also supports the onclick attribute:

<input type="text" onClick="alert('Hello World')" id="myTextbox" />

The additionalAttributes is especially helpful if only a few of these additional attributes are needed. Otherwise, it is often reasonable to write an own Viewhelper which extends the corresponding Viewhelper.

additionalAttributes is provided by the TagBasedViewHelper, the base class for tag-based Viewhelpers (see appendix C), and it allows the adding of optional attributes for the HTML tag output.