The <label> HTML element represents a caption for an item in a user interface.
Instantiate the Label class using Label::widget().
$label = Label::widget();Use the provided methods to set specific attributes for the a element.
// setting class attribute
$label->class('text-primary');Or, use the attributes method to set multiple attributes at once.
$label->attributes(['class' => 'text-primary', 'title' => 'Home']);If you want to include content within the label tag, use the content method.
$label->content('MyContent');Generate the HTML output using the render method.
$html = $label->render();Or, use the magic __toString method.
$html = (string) $label;Below are examples of common use cases:
// adding multiple attributes
$label->class('text-primary')->title('Home');
// using data attributes
$label->dataAttributes(['bs-toggle' => 'modal', 'bs-target' => '#exampleModal', 'analytics' => 'trackClick']);Explore additional methods for setting various attributes such as for, form, lang, tabindex, title, and more.
Use prefix and suffix methods to add text before and after the label tag, respectively.
// adding a prefix
$html = $label->content('MyContent')->prefix('MyPrefix')->render();
// adding a suffix
$html = $label->content('MyContent')->suffix('MySuffix')->render();The template method allows you to customize the HTML output of the a element.
The following template tags are available:
| Tag | Description |
|---|---|
{prefix} |
The prefix text. |
{tag} |
The a element. |
{suffix} |
The suffix text. |
// using a custom template
$label->template('<span>{tag}</span>');Refer to the Attribute Tests for comprehensive examples.
The following methods are available for setting attributes:
| Method | Description |
|---|---|
attributes() |
Set multiple attributes at once. |
class() |
Set the class attribute. |
content() |
Set the content within the label element. |
dataAttributes() |
Set multiple data-attributes at once. |
for() |
Set the for attribute. |
form() |
Set the form attribute. |
id() |
Set the id attribute. |
lang() |
Set the lang attribute. |
name() |
Set the name attribute. |
style() |
Set the style attribute. |
title() |
Set the title attribute. |
Refer to the Custom Method Test for comprehensive examples.
The following methods are available for customizing the HTML output:
| Method | Description |
|---|---|
prefix() |
Add text before the label element. |
prefixContainer() |
Set enabled or disabled for the prefix-container element. |
prefixContainerAttributes() |
Set attributes for the prefix-container element. |
prefixContainerClass() |
Set the class attribute for the prefix-container element. |
prefixContainerTag() |
Set the tag for the prefix-container element. |
render() |
Generates the HTML output. |
suffix() |
Add text after the label element. |
suffixContainer() |
Set enabled or disabled for the suffix-container element. |
suffixContainerAttributes() |
Set attributes for the suffix-container element. |
suffixContainerClass() |
Set the class attribute for the suffix-container element. |
suffixContainerTag() |
Set the tag for the suffix-container element. |
template() |
Set the template for the label element. |
widget() |
Instantiates the Label::class. |