The htmlBuilder interface is used create HTML markup. It makes it easy to generate markup that is well formed and properly escaped. It is simpler and safer than using string concatenation and doesn't require the overhead of using a template library. For simple templates see apex.util.applyTemplate
Example
var out = apex.util.htmlBuilder();
out.markup( "<label" )
.attr( "for", options.id )
.markup( ">" )
.content( option.label )
.markup( "</label><input type='text'" )
.attr( "id", options.id )
.attr( "class", "specialInput" )
.optionalAttr( "title", options.title )
.attr( "size", options.size )
.attr( "maxlength", options.maxChars )
.attr( "value", "" )
.markup( " />" );
$( "#myContainer", out.toString() );
Methods
attr(pNameopt, pValue) → {this}
Add an attribute.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
pName |
string |
<optional> |
Attribute name. A leading space and trailing = is added and the value is quoted. If not given just the value is added without being quoted. |
pValue |
string | Attribute value. This will be escaped. |
Returns:
- Type
- this
clear()
Remove all markup from this builder interface instance. Use this when you want to reuse the builder instance for new markup.
content(pContent) → {this}
Add element content. The content is escaped.
Parameters:
Name | Type | Description |
---|---|---|
pContent |
string | The content to add between an element open and closing tags. |
Returns:
- Type
- this
markup(pMarkup) → {this}
Add markup.
Parameters:
Name | Type | Description |
---|---|---|
pMarkup |
string | The markup to add. No escaping is done. |
Returns:
- Type
- this
optionalAttr(pName, pValue) → {this}
Add an optional attribute. The attribute and its value is only added if the value is a non-empty string or a non-zero number or true.
Parameters:
Name | Type | Description |
---|---|---|
pName |
string | Attribute name. A leading space and trailing = is added and the value is quoted. |
pValue |
string | Attribute value. This will be escaped. |
Returns:
- Type
- this
optionalBoolAttr(pName, pValue) → {this}
Add an optional Boolean attribute. The attribute is added only if the value is true.
Parameters:
Name | Type | Description |
---|---|---|
pName |
string | Attribute name. A leading space is added. |
pValue |
boolean | If true the attribute is added. If false the attribute is not added. |
Returns:
- Type
- this
toString() → {string}
Return the HTML markup.
Returns:
- Type
- string