Html ​
- Type
- Class
- Namespace
- craft\helpers
- Inherits
- craft\helpers\Html » yii\helpers\Html » yii\helpers\BaseHtml
- Since
- 3.0.0
Class Html
Public Properties ​
Property | Description |
---|---|
attributeOrder | array – The preferred order of attributes in a tag. |
attributeRegex | string – Regular expression used for attribute name validation. |
dataAttributes | array – List of tag attributes that should be specially handled when their values are of array type. |
normalizeClassAttribute | boolean – Whether to removes duplicate class names in tag attribute class |
voidElements | array – List of void elements (element name => 1) |
Public Methods ​
Method | Description |
---|---|
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. |
dataUrl() | Generates a base64-encoded data URL for the given file path. |
dataUrlFromString() | Generates a base64-encoded data URL based on the given file contents and MIME type. |
decode() | Decodes special HTML entities back to the corresponding characters. |
dropDownList() | Generates a drop-down list. |
encode() | 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() | 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. |
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() | 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. |
getInputIdByName() | Converts input name to ID. |
getInputName() | Generates an appropriate input name for the specified attribute name or expression. |
hiddenInput() | Generates a hidden input field. |
id() | Normalizes an element ID into only alphanumeric characters, underscores, and dashes, or generates one at random. |
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(). |
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() | 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() | 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. |
sanitizeSvg() | Sanitizes an SVG. |
script() | Generates a script tag. |
style() | Generates a style tag. |
submitButton() | Generates a submit button tag. |
submitInput() | Generates a submit input button. |
successMessageInput() | Generates a hidden successMessage input tag. |
tag() | Generates a complete HTML tag. |
textInput() | Generates a text input field. |
textarea() | Generates a text area input. |
ul() | 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.
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 yii\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 yii\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.
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.
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.
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
dataUrl()
​
- Since
- 3.5.13
Generates a base64-encoded data URL for the given file path.
Arguments ​
$file
(string) – The file path$mimeType
(string, null) – The file’s MIME type. Ifnull
then it will be determined automatically.
Returns ​
string – The data URL
Throws ​
- yii\base\InvalidArgumentException
if$file
is an invalid file path
dataUrlFromString()
​
- Since
- 3.5.13
Generates a base64-encoded data URL based on the given file contents and MIME type.
Arguments ​
$contents
(string) – The file path$mimeType
(string, null) – The file’s MIME type. Ifnull
then it will be determined automatically.
Returns ​
string – The data URL
Throws ​
- yii\base\InvalidArgumentException
if$file
is an invalid file path
encodeInvalidTags()
​
- Since
- 3.7.27
Encodes invalid (unclosed) HTML tags so they appear as plain text.
Arguments ​
$html
(string)
Returns ​
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.
Arguments ​
$html
(string) – The HTML string.$variables
(array) – An associative array of key => value pairs to be applied to the HTML string usingstrtr
.
Returns ​
string – The HTML string with the encoded variable values swapped in.
explodeClass()
​
- Since
- 3.5.0
Explodes a class
attribute into an array.
Arguments ​
Returns ​
string[]
explodeStyle()
​
- Since
- 3.5.0
Explodes a style
attribute into an array of property/value pairs.
Arguments ​
Returns ​
string[]
failMessageInput()
​
- Since
- 3.6.6
Generates a hidden failMessage
input tag.
Arguments ​
$message
(string) – The flash message to shown on failure$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 ​
- yii\base\Exception
if the validation key could not be written - yii\base\InvalidConfigException
when HMAC generation fails
id()
​
- Since
- 3.5.0
Normalizes an element ID into only alphanumeric characters, underscores, and dashes, or generates one at random.
Arguments ​
$id
(string)
Returns ​
modifyTagAttributes()
​
- Since
- 3.3.0
Modifies a HTML tag’s attributes, supporting the same attribute definitions as renderTagAttributes().
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 ​
- yii\base\InvalidArgumentException
if$tag
doesn't contain a valid HTML tag
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
, anddata-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:
Arguments ​
$html
(string) – The HTML code$namespace
(string) – The namespace$withClasses
(boolean) – Whether class names should be namespaced as well (affects bothclass
attributes and class name CSS selectors)
Returns ​
string – 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().
Arguments ​
$html
(string) – The HTML code$namespace
(string) – The namespace$withClasses
(boolean) – Whether class names should be namespaced as well (affects bothclass
attributes and class name CSS selectors)
Returns ​
string – The HTML with namespaced attributes
namespaceId()
​
- Since
- 3.5.0
Namespaces an ID.
Arguments ​
Returns ​
string – The namespaced ID
namespaceInputName()
​
- Since
- 3.5.0
Namespaces an input name.
Arguments ​
Returns ​
string – 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:
Arguments ​
Returns ​
string – The HTML with namespaced input names
normalizeTagAttributes()
​
- Since
- 3.3.0
Normalizes attributes.
Arguments ​
$attributes
(array)
Returns ​
parseTag()
​
- Since
- 3.3.0
Parses an HTML tag and returns info about it and its children.
Arguments ​
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 ​
- craft\errors\InvalidHtmlTagException
if$tag
doesn't contain a valid HTML tag
parseTagAttribute()
​
- Since
- 3.7.0
Parses the next HTML tag attribute in a given string.
Arguments ​
$html
(string) – The HTML to parse$offset
(integer) – The offset to start looking for an attribute$start
(integer, null) – The start position of the attribute in the given HTML$end
(integer, null) – The end position of the attribute in the given HTML
Returns ​
array, null – The name and value of the attribute, or false
if no complete attribute was found
Throws ​
- yii\base\InvalidArgumentException
if$html
doesn't begin with a valid HTML attribute
parseTagAttributes()
​
- Since
- 3.3.0
Parses an HTML tag to find its attributes.
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 ​
- craft\errors\InvalidHtmlTagException
if$tag
doesn't contain a valid HTML tag
prependToTag()
​
- Since
- 3.3.0
Prepends HTML to the beginning of given tag.
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.
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 ​
- yii\base\Exception
if the validation key could not be written - yii\base\InvalidConfigException
when HMAC generation fails
sanitizeSvg()
​
- Since
- 3.5.0
Sanitizes an SVG.
Arguments ​
$svg
(string)
Returns ​
successMessageInput()
​
- Since
- 3.6.6
Generates a hidden successMessage
input tag.
Arguments ​
$message
(string) – The flash message to shown on success$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 ​
- yii\base\Exception
if the validation key could not be written - yii\base\InvalidConfigException
when HMAC generation fails
widont()
​
- Since
- 3.7.0
Inserts a non-breaking space between the last two words of a string.
Arguments ​
$string
(string)
Returns ​
Protected Methods ​
Method | Description |
---|---|
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. |