Html

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\Html » yii\helpers\Html (opens new window) » yii\helpers\BaseHtml (opens new window)
Since
3.0.0

Class Html

View source (opens new window)

# Public Properties

Property Description
attributeOrder (opens new window) array (opens new window) – The preferred order of attributes in a tag.
attributeRegex (opens new window) string (opens new window) – Regular expression used for attribute name validation.
dataAttributes (opens new window) array (opens new window) – List of tag attributes that should be specially handled when their values are of array type.
normalizeClassAttribute (opens new window) boolean (opens new window) – Whether to removes duplicate class names in tag attribute class
voidElements (opens new window) array (opens new window) – List of void elements (element name => 1)

# Public Methods

Method Description
a() Generates a hyperlink tag.
actionInput() Generates a hidden action input tag.
activeCheckbox() (opens new window) Generates a checkbox tag together with a label for the given model attribute.
activeCheckboxList() (opens new window) Generates a list of checkboxes.
activeDropDownList() (opens new window) Generates a drop-down list for the given model attribute.
activeFileInput() (opens new window) Generates a file input tag for the given model attribute.
activeHiddenInput() (opens new window) Generates a hidden input tag for the given model attribute.
activeHint() (opens new window) Generates a hint tag for the given model attribute.
activeInput() (opens new window) Generates an input tag for the given model attribute.
activeLabel() (opens new window) Generates a label tag for the given model attribute.
activeListBox() (opens new window) Generates a list box.
activePasswordInput() (opens new window) Generates a password input tag for the given model attribute.
activeRadio() (opens new window) Generates a radio button tag together with a label for the given model attribute.
activeRadioList() (opens new window) Generates a list of radio buttons.
activeTextInput() (opens new window) Generates a text input tag for the given model attribute.
activeTextarea() (opens new window) Generates a textarea tag for the given model attribute.
addCssClass() (opens new window) Adds a CSS class (or several classes) to the specified options.
addCssStyle() (opens new window) Adds the specified CSS style to the HTML options.
appendToTag() Appends HTML to the end of the given tag.
beginForm() (opens new window) Generates a form start tag.
beginTag() (opens new window) Generates a start tag.
button() (opens new window) Generates a button tag.
buttonInput() (opens new window) Generates an input button.
checkbox() (opens new window) Generates a checkbox input.
checkboxList() (opens new window) Generates a list of checkboxes.
csrfInput() Generates a hidden CSRF input tag.
csrfMetaTags() (opens new window) Generates the meta tags containing CSRF token information.
cssFile() (opens new window) Generates a link tag that refers to an external CSS file.
cssStyleFromArray() (opens new window) Converts a CSS style array into a string representation.
cssStyleToArray() (opens new window) Converts a CSS style string into an array representation.
dataUrl() Generates a base64-encoded data URL (opens new window) for the given file path.
dataUrlFromString() Generates a base64-encoded data URL (opens new window) based on the given file contents and MIME type.
decode() (opens new window) Decodes special HTML entities back to the corresponding characters.
dropDownList() (opens new window) Generates a drop-down list.
encode() (opens new window) Encodes special characters into HTML entities.
encodeInvalidTags() Encodes invalid (unclosed) HTML tags so they appear as plain text.
encodeParams() Will take an HTML string and an associative array of key=>value pairs, HTML encode the values and swap them back into the original string using the keys as tokens.
endForm() (opens new window) Generates a form end tag.
endTag() (opens new window) Generates an end tag.
error() (opens new window) Generates a tag that contains the first validation error of the specified model attribute.
errorSummary() (opens new window) Generates a summary of the validation errors.
escapeJsRegularExpression() (opens new window) Escapes regular expression to use in JavaScript.
explodeClass() Explodes a class attribute into an array.
explodeStyle() Explodes a style attribute into an array of property/value pairs.
failMessageInput() Generates a hidden failMessage input tag.
fileInput() (opens new window) Generates a file input field.
getAttributeName() (opens new window) Returns the real attribute name from the given attribute expression.
getAttributeValue() (opens new window) Returns the value of the specified attribute name or expression.
getInputId() (opens new window) Generates an appropriate input ID for the specified attribute name or expression.
getInputIdByName() (opens new window) Converts input name to ID.
getInputName() (opens new window) Generates an appropriate input name for the specified attribute name or expression.
hiddenInput() (opens new window) Generates a hidden input field.
id() Normalizes an element ID into only alphanumeric characters, underscores, and dashes, or generates one at random.
img() (opens new window) Generates an image tag.
input() (opens new window) Generates an input type of the given type.
jsFile() (opens new window) Generates a script tag that refers to an external JavaScript file.
label() (opens new window) Generates a label tag.
listBox() (opens new window) Generates a list box.
mailto() (opens new window) Generates a mailto hyperlink.
modifyTagAttributes() Modifies a HTML tag’s attributes, supporting the same attribute definitions as renderTagAttributes() (opens new window).
namespaceAttributes() Prepends a namespace to id attributes, and any of the following things that reference those IDs:
namespaceHtml() Namespaces input names and other HTML attributes, as well as CSS selectors.
namespaceId() Namespaces an ID.
namespaceInputName() Namespaces an input name.
namespaceInputs() Renames HTML input names so they belong to a namespace.
normalizeTagAttributes() Normalizes attributes.
ol() (opens new window) Generates an ordered list.
parseTag() Parses an HTML tag and returns info about it and its children.
parseTagAttribute() Parses the next HTML tag attribute in a given string.
parseTagAttributes() Parses an HTML tag to find its attributes.
passwordInput() (opens new window) Generates a password input field.
prependToTag() Prepends HTML to the beginning of given tag.
radio() (opens new window) Generates a radio button input.
radioList() (opens new window) Generates a list of radio buttons.
redirectInput() Generates a hidden redirect input tag.
removeCssClass() (opens new window) Removes a CSS class from the specified options.
removeCssStyle() (opens new window) Removes the specified CSS style from the HTML options.
renderSelectOptions() (opens new window) Renders the option tags that can be used by dropDownList() (opens new window) and listBox() (opens new window).
renderTagAttributes() (opens new window) Renders the HTML tag attributes.
resetButton() (opens new window) Generates a reset button tag.
resetInput() (opens new window) Generates a reset input button.
sanitizeSvg() Sanitizes an SVG.
script() (opens new window) Generates a script tag.
style() (opens new window) Generates a style tag.
submitButton() (opens new window) Generates a submit button tag.
submitInput() (opens new window) Generates a submit input button.
successMessageInput() Generates a hidden successMessage input tag.
tag() (opens new window) Generates a complete HTML tag.
textInput() (opens new window) Generates a text input field.
textarea() (opens new window) Generates a text area input.
ul() (opens new window) Generates an unordered list.
widont() Inserts a non-breaking space between the last two words of a string.

# a()

Since
3.3.0

Generates a hyperlink tag.

View source (opens new window)

Arguments

If you want to use an absolute url you can call yii\helpers\Url::to() (opens new window) yourself, before passing the URL to this method, like this:

Html::a('link text', Url::to($url, true))

Returns

string (opens new window) – The generated hyperlink

# actionInput()

Since
3.3.0

Generates a hidden action input tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The generated hidden input tag

# appendToTag()

Since
3.3.0

Appends HTML to the end of the given tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The modified HTML

# csrfInput()

Since
3.3.0

Generates a hidden CSRF input tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The generated hidden input tag

# dataUrl()

Since
3.5.13

Generates a base64-encoded data URL (opens new window) for the given file path.

View source (opens new window)

Arguments

Returns

string (opens new window) – The data URL

Throws

# dataUrlFromString()

Since
3.5.13

Generates a base64-encoded data URL (opens new window) based on the given file contents and MIME type.

View source (opens new window)

Arguments

Returns

string (opens new window) – The data URL

Throws

# encodeInvalidTags()

Since
3.7.27

Encodes invalid (unclosed) HTML tags so they appear as plain text.

View source (opens new window)

Arguments

Returns

string (opens new window)

# encodeParams()

Will take an HTML string and an associative array of key=>value pairs, HTML encode the values and swap them back into the original string using the keys as tokens.

View source (opens new window)

Arguments

Returns

string (opens new window) – The HTML string with the encoded variable values swapped in.

# explodeClass()

Since
3.5.0

Explodes a class attribute into an array.

View source (opens new window)

Arguments

Returns

string (opens new window)[]

# explodeStyle()

Since
3.5.0

Explodes a style attribute into an array of property/value pairs.

View source (opens new window)

Arguments

Returns

string (opens new window)[]

# failMessageInput()

Since
3.6.6

Generates a hidden failMessage input tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The generated hidden input tag

Throws

# id()

Since
3.5.0

Normalizes an element ID into only alphanumeric characters, underscores, and dashes, or generates one at random.

View source (opens new window)

Arguments

Returns

string (opens new window)

# modifyTagAttributes()

Since
3.3.0

Modifies a HTML tag’s attributes, supporting the same attribute definitions as renderTagAttributes() (opens new window).

View source (opens new window)

Arguments

Returns

string (opens new window) – The modified HTML tag.

Throws

# namespaceAttributes()

Since
3.5.0

Prepends a namespace to id attributes, and any of the following things that reference those IDs:

  • for, list, href, aria-labelledby, aria-describedby, aria-controls, data-target, data-reverse-target, and data-target-prefix attributes
  • ID selectors within <style> tags

For example, this:

<style>#summary { font-size: larger }</style>
<p id="summary">...</p>

would become this, if it were namespaced with foo:

<style>#foo-summary { font-size: larger }</style>
<p id="foo-summary">...</p>

See also:

View source (opens new window)

Arguments

Returns

string (opens new window) – The HTML with namespaced attributes

# namespaceHtml()

Since
3.5.0

Namespaces input names and other HTML attributes, as well as CSS selectors.

This is a shortcut for calling namespaceInputs() and namespaceAttributes().

View source (opens new window)

Arguments

Returns

string (opens new window) – The HTML with namespaced attributes

# namespaceId()

Since
3.5.0

Namespaces an ID.

View source (opens new window)

Arguments

Returns

string (opens new window) – The namespaced ID

# namespaceInputName()

Since
3.5.0

Namespaces an input name.

View source (opens new window)

Arguments

Returns

string (opens new window) – The namespaced input name

# namespaceInputs()

Since
3.5.0

Renames HTML input names so they belong to a namespace.

This method will go through the passed-in HTML code looking for name attributes, and namespace their values.

For example, this:

<input type="text" name="title">
<textarea name="fields[body]"></textarea>

would become this, if it were namespaced with foo:

<input type="text" name="foo[title]">
<textarea name="foo[fields][body]"></textarea>

See also:

View source (opens new window)

Arguments

Returns

string (opens new window) – The HTML with namespaced input names

# normalizeTagAttributes()

Since
3.3.0

Normalizes attributes.

View source (opens new window)

Arguments

Returns

array (opens new window)

# parseTag()

Since
3.3.0

Parses an HTML tag and returns info about it and its children.

View source (opens new window)

Arguments

Returns

array (opens new window) – An array containing type, attributes, children, start, end, htmlStart, and htmlEnd properties. Nested text nodes will be represented as arrays within children with type set to 'text', and a value key containing the text value.

Throws

# parseTagAttribute()

Since
3.7.0

Parses the next HTML tag attribute in a given string.

View source (opens new window)

Arguments

Returns

array (opens new window), null (opens new window) – The name and value of the attribute, or false if no complete attribute was found

Throws

# parseTagAttributes()

Since
3.3.0

Parses an HTML tag to find its attributes.

View source (opens new window)

Arguments

Returns

array (opens new window) – The parsed HTML tag attributes

Throws

# prependToTag()

Since
3.3.0

Prepends HTML to the beginning of given tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The modified HTML

# redirectInput()

Since
3.3.0

Generates a hidden redirect input tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The generated hidden input tag

Throws

# sanitizeSvg()

Since
3.5.0

Sanitizes an SVG.

View source (opens new window)

Arguments

Returns

string (opens new window)

# successMessageInput()

Since
3.6.6

Generates a hidden successMessage input tag.

View source (opens new window)

Arguments

Returns

string (opens new window) – The generated hidden input tag

Throws

# widont()

Since
3.7.0

Inserts a non-breaking space between the last two words of a string.

View source (opens new window)

Arguments

Returns

string (opens new window)

# Protected Methods

Method Description
activeBooleanInput() (opens new window) Generates a boolean input This method is mainly called by activeCheckbox() (opens new window) and activeRadio() (opens new window).
activeListInput() (opens new window) Generates a list of input fields.
booleanInput() (opens new window) Generates a boolean input.
setActivePlaceholder() (opens new window) Generate placeholder from model attribute label.