Html

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\Html » yii\helpers\Html » yii\helpers\BaseHtml
Since
3.0.0

Class Html

View source

Public Properties

PropertyDescription
attributeOrderarray – The preferred order of attributes in a tag.
attributeRegexstring – Regular expression used for attribute name validation.
dataAttributesarray – List of tag attributes that should be specially handled when their values are of array type.
voidElementsarray – List of void elements (element name => 1)

Public Methods

MethodDescription
a()Generates a hyperlink tag.
actionInput()Generates a hidden action input tag.
activeCheckbox()Generates a checkbox tag together with a label for the given model attribute.
activeCheckboxList()Generates a list of checkboxes.
activeDropDownList()Generates a drop-down list for the given model attribute.
activeFileInput()Generates a file input tag for the given model attribute.
activeHiddenInput()Generates a hidden input tag for the given model attribute.
activeHint()Generates a hint tag for the given model attribute.
activeInput()Generates an input tag for the given model attribute.
activeLabel()Generates a label tag for the given model attribute.
activeListBox()Generates a list box.
activePasswordInput()Generates a password input tag for the given model attribute.
activeRadio()Generates a radio button tag together with a label for the given model attribute.
activeRadioList()Generates a list of radio buttons.
activeTextInput()Generates a text input tag for the given model attribute.
activeTextarea()Generates a textarea tag for the given model attribute.
addCssClass()Adds a CSS class (or several classes) to the specified options.
addCssStyle()Adds the specified CSS style to the HTML options.
appendToTag()Appends HTML to the end of the given tag.
beginForm()Generates a form start tag.
beginTag()Generates a start tag.
button()Generates a button tag.
buttonInput()Generates an input button.
checkbox()Generates a checkbox input.
checkboxList()Generates a list of checkboxes.
csrfInput()Generates a hidden CSRF input tag.
csrfMetaTags()Generates the meta tags containing CSRF token information.
cssFile()Generates a link tag that refers to an external CSS file.
cssStyleFromArray()Converts a CSS style array into a string representation.
cssStyleToArray()Converts a CSS style string into an array representation.
decode()Decodes special HTML entities back to the corresponding characters.
dropDownList()Generates a drop-down list.
encode()Encodes special characters into HTML entities.
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()Generates a form end tag.
endTag()Generates an end tag.
error()Generates a tag that contains the first validation error of the specified model attribute.
errorSummary()Generates a summary of the validation errors.
escapeJsRegularExpression()Escapes regular expression to use in JavaScript.
fileInput()Generates a file input field.
getAttributeName()Returns the real attribute name from the given attribute expression.
getAttributeValue()Returns the value of the specified attribute name or expression.
getInputId()Generates an appropriate input ID for the specified attribute name or expression.
getInputName()Generates an appropriate input name for the specified attribute name or expression.
hiddenInput()Generates a hidden input field.
img()Generates an image tag.
input()Generates an input type of the given type.
jsFile()Generates a script tag that refers to an external JavaScript file.
label()Generates a label tag.
listBox()Generates a list box.
mailto()Generates a mailto hyperlink.
modifyTagAttributes()Modifies a HTML tag’s attributes, supporting the same attribute definitions as renderTagAttributes().
normalizeTagAttributes()Normalizes attributes.
ol()Generates an ordered list.
parseTag()Parses an HTML tag and returns info about it and its children.
parseTagAttributes()Parses an HTML tag to find its attributes.
passwordInput()Generates a password input field.
prependToTag()Prepends HTML to the beginning of given tag.
radio()Generates a radio button input.
radioList()Generates a list of radio buttons.
redirectInput()Generates a hidden redirect input tag.
removeCssClass()Removes a CSS class from the specified options.
removeCssStyle()Removes the specified CSS style from the HTML options.
renderSelectOptions()Renders the option tags that can be used by dropDownList() and listBox().
renderTagAttributes()Renders the HTML tag attributes.
resetButton()Generates a reset button tag.
resetInput()Generates a reset input button.
script()Generates a script tag.
style()Generates a style tag.
submitButton()Generates a submit button tag.
submitInput()Generates a submit input button.
tag()Generates a complete HTML tag.
textInput()Generates a text input field.
textarea()Generates a text area input.
ul()Generates an unordered list.

a()

Since
3.3.0

Generates a hyperlink tag.

View source

Arguments

  • $text (string) – Link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag. If this is coming from end users, you should consider encode() it to prevent XSS attacks.
  • $url (array, string, null) – The URL for the hyperlink tag. This parameter will be processed by \craft\helpers\Url::to() and will be used for the "href" attribute of the tag. If this parameter is null, the "href" attribute will not be generated.

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

Html::a('link text', Url::to($url, true))
  • $options (array) – The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using encode(). If a value is null, the corresponding attribute will not be rendered. See renderTagAttributes() for details on how attributes are being rendered.

Returns

string – The generated hyperlink

actionInput()

Since
3.3.0

Generates a hidden action input tag.

View source

Arguments

  • $route (string) – The action route
  • $options (array) – The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using encode(). If a value is null, the corresponding attribute will not be rendered. See renderTagAttributes() for details on how attributes are being rendered.

Returns

string – The generated hidden input tag

appendToTag()

Since
3.3.0

Appends HTML to the end of the given tag.

View source

Arguments

  • $tag (string) – The HTML tag that $html should be appended to
  • $html (string) – The HTML to append to $tag.
  • $ifExists (string, null) – What to do if $tag already contains a child of the same type as the element defined by $html. Set to 'keep' if no action should be taken, or 'replace' if it should be replaced by $tag.

Returns

string – The modified HTML

csrfInput()

Since
3.3.0

Generates a hidden CSRF input tag.

View source

Arguments

  • $options (array) – The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using encode(). If a value is null, the corresponding attribute will not be rendered. See renderTagAttributes() for details on how attributes are being rendered.

Returns

string – The generated hidden input tag

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

Arguments

  • $html (string) – The HTML string.
  • $variables (array) – An associative array of key => value pairs to be applied to the HTML string using strtr.

Returns

string – The HTML string with the encoded variable values swapped in.

modifyTagAttributes()

Since
3.3.0

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

View source

Arguments

  • $tag (string) – The HTML tag whose attributes should be modified.
  • $attributes (array) – The attributes to be added to the tag.

Returns

string – The modified HTML tag.

Throws

normalizeTagAttributes()

Since
3.3.0

Normalizes attributes.

View source

Arguments

Returns

array

parseTag()

Since
3.3.0

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

View source

Arguments

  • $tag (string) – The HTML tag
  • $offset (integer) – The offset to start looking for a tag

Returns

array – 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

parseTagAttributes()

Since
3.3.0

Parses an HTML tag to find its attributes.

View source

Arguments

  • $tag (string) – The HTML tag to parse
  • $offset (integer) – The offset to start looking for a tag
  • $start (integer, null) – The start position of the first attribute in the given tag
  • $end (integer, null) – The end position of the last attribute in the given tag
  • $decode (boolean) – Whether the attributes should be HTML decoded in the process

Returns

array – The parsed HTML tag attributes

Throws

prependToTag()

Since
3.3.0

Prepends HTML to the beginning of given tag.

View source

Arguments

  • $tag (string) – The HTML tag that $html should be prepended to
  • $html (string) – The HTML to prepend to $tag.
  • $ifExists (string, null) – What to do if $tag already contains a child of the same type as the element defined by $html. Set to 'keep' if no action should be taken, or 'replace' if it should be replaced by $tag.

Returns

string – The modified HTML

redirectInput()

Since
3.3.0

Generates a hidden redirect input tag.

View source

Arguments

  • $url (string) – The URL to redirect to
  • $options (array) – The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using encode(). If a value is null, the corresponding attribute will not be rendered. See renderTagAttributes() for details on how attributes are being rendered.

Returns

string – The generated hidden input tag

Throws

Protected Methods

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