HtmlHelper
extends HtmlHelper
in package
uses
ClassTrait, EasyIconTrait
Html Helper class for easy use of HTML widgets.
HtmlHelper encloses all methods needed while working with HTML pages.
Tags
Table of Contents
Properties
- $easyIcon : bool
- Set to false to disable easy icon processing.
- $Url : UrlHelper
- $_defaultConfig : array<string|int, mixed>
- Default config for the helper.
Methods
- addClass() : array<string|int, mixed>
- Adds the given class to the element options.
- alert() : string
- Create a Twitter Bootstrap style alert block, containing text.
- badge() : string
- Create a Twitter Bootstrap badge.
- dropdown() : string
- Create & return a twitter bootstrap dropdown menu.
- icon() : string
- Create an icon using the template `icon`.
- link() : string
- {@inheritDoc}
- progress() : string
- Create a Twitter Bootstrap style progress bar.
- tooltip() : string
- Create a Twitter Bootstrap style tooltip.
- _addButtonClasses() : array<string|int, mixed>
- Add classes to options according to the default values of bootstrap-type and bootstrap-size for button (see configuration).
- _easyIconOption() : array<string|int, mixed>
- Remove the `easyIcon` option from the given array and return it together with the array.
- _injectIcon() : string
- Inject icon into the given string.
- _isAssociativeArray() : bool
- Check weither the specified array is associative or not.
- _makeIcon() : string
- Try to convert the specified string to a bootstrap icon. The string is converted if it matches a format `i:icon-name` (leading and trailing spaces or ignored) and if easy-icon is activated.
Properties
$easyIcon
Set to false to disable easy icon processing.
public
bool
$easyIcon
= true
$Url
public
UrlHelper
$Url
$_defaultConfig
Default config for the helper.
protected
array<string|int, mixed>
$_defaultConfig
= ["templates" => [
"meta" => "<meta{{attrs}}/>",
"metalink" => '<link href="{{url}}"{{attrs}}/>',
"link" => '<a href="{{url}}"{{attrs}}>{{content}}</a>',
"mailto" => '<a href="mailto:{{url}}"{{attrs}}>{{content}}</a>',
"image" => '<img src="{{url}}"{{attrs}}/>',
"tableheader" => "<th{{attrs}}>{{content}}</th>",
"tableheaderrow" => "<tr{{attrs}}>{{content}}</tr>",
"tablecell" => "<td{{attrs}}>{{content}}</td>",
"tablerow" => "<tr{{attrs}}>{{content}}</tr>",
"block" => "<div{{attrs}}>{{content}}</div>",
"blockstart" => "<div{{attrs}}>",
"blockend" => "</div>",
"tag" => "<{{tag}}{{attrs}}>{{content}}</{{tag}}>",
"tagstart" => "<{{tag}}{{attrs}}>",
"tagend" => "</{{tag}}>",
"tagselfclosing" => "<{{tag}}{{attrs}}/>",
"para" => "<p{{attrs}}>{{content}}</p>",
"parastart" => "<p{{attrs}}>",
"css" => '<link rel="{{rel}}" href="{{url}}"{{attrs}}/>',
"style" => "<style{{attrs}}>{{content}}</style>",
"charset" => '<meta charset="{{charset}}"/>',
"ul" => "<ul{{attrs}}>{{content}}</ul>",
"ol" => "<ol{{attrs}}>{{content}}</ol>",
"li" => "<li{{attrs}}>{{content}}</li>",
"javascriptblock" => "<script{{attrs}}>{{content}}</script>",
"javascriptstart" => "<script>",
"javascriptlink" => '<script src="{{url}}"{{attrs}}></script>',
"javascriptend" => "</script>",
// New templates for Bootstrap
"icon" => '<i aria-hidden="true" class="fa fa-{{type}}{{attrs.class}}"{{attrs}}></i>',
"badge" => '<span class="badge badge-{{type}}{{attrs.class}}"{{attrs}}>{{content}}</span>',
"alert" => '<div class="alert alert-{{type}}{{attrs.class}}" role="alert"{{attrs}}>{{close}}{{content}}</div>',
"alertCloseButton" => '<button type="button" class="close{{attrs.class}}" data-dismiss="alert" aria-label="{{label}}"{{attrs}}>{{content}}</button>',
"alertCloseContent" => '<span aria-hidden="true">×</span>',
"tooltip" => '<{{tag}} data-toggle="{{toggle}}" data-placement="{{placement}}" title="{{tooltip}}"{{attrs}}>{{content}}</{{tag}}>',
"progressBar" => '<div class="progress-bar bg-{{type}}{{attrs.class}}" role="progressbar"
aria-valuenow="{{width}}" aria-valuemin="{{min}}" aria-valuemax="{{max}}" style="width: {{width}}%;"{{attrs}}>{{inner}}</div>',
"progressBarInner" => '<span class="sr-only">{{width}}%</span>',
"progressBarContainer" => '<div class="progress{{attrs.class}}"{{attrs}}>{{content}}</div>',
"dropdownMenu" => '<div class="dropdown-menu{{attrs.class}}"{{attrs}}>{{content}}</div>',
"dropdownMenuItem" => '<a href="{{url}}" class="dropdown-item{{attrs.class}}"{{attrs}}>{{content}}</a>',
"dropdownMenuHeader" => '<h6 class="dropdown-header{{attrs.class}}"{{attrs}}>{{content}}</h6>',
"dropdownMenuDivider" => '<div role="separator" class="dropdown-divider{{attrs.class}}"{{attrs}}></div>',
"confirmJs" => "{{confirm}}",
], "templateClass" => "Bootstrap\\View\\EnhancedStringTemplate", "tooltip" => ["tag" => "span", "placement" => "right", "toggle" => "tooltip"], "badge" => ["type" => "default"], "alert" => ["type" => "warning", "close" => true], "progress" => ["type" => "primary"]]
Options:
alertDefault options for alert.labelDefault options for labels.progressDefault options for progress bar.tooltipDefault options for tooltips.- See CakePHP documentation for extra configuration options.
Methods
addClass()
Adds the given class to the element options.
public
addClass([array<string|int, mixed> $options = [] ][, string|array<string|int, mixed> $class = null ][, string $key = "class" ]) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed> = []
-
Array of options/attributes to add a class to.
- $class : string|array<string|int, mixed> = null
-
The class names to be added.
- $key : string = "class"
-
The key to use for class (default to
'class').
Return values
array<string|int, mixed> —Array of options with $key set or modified.
alert()
Create a Twitter Bootstrap style alert block, containing text.
public
alert(string $text[, string|array<string|int, mixed>|null $type = null ][, array<string|int, mixed> $options = [] ]) : string
The second parameter may either be $type or $options (in this case,
the third parameter is not used, and the alert type can be specified in the
$options array).
Options
closeDismissible alert. See configuration for default.typeThe type of the alert. See configuration for default.templateVarsProvide template variables for thealerttemplate.- Other attributes will be assigned to the wrapper element.
Parameters
- $text : string
-
The alert text.
- $type : string|array<string|int, mixed>|null = null
-
The type of the alert.
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —A HTML bootstrap alert element.
badge()
Create a Twitter Bootstrap badge.
public
badge(string $text[, string|array<string|int, mixed> $type = null ][, array<string|int, mixed> $options = [] ]) : string
Options
templateVarsProvide template variables for thebadgetemplate.- Other attributes will be assigned to the wrapper element.
Parameters
- $text : string
-
The badge text.
- $type : string|array<string|int, mixed> = null
-
The badge type (default, primary, secondary, success, warning, info, danger) or the array of options (see
$options). - $options : array<string|int, mixed> = []
-
Array of options. See above. Default values are retrieved from the configuration.
Return values
stringdropdown()
Create & return a twitter bootstrap dropdown menu.
public
dropdown([array<string|int, mixed> $menu = [] ][, array<string|int, mixed> $options = [] ]) : string
[
// Divider
'divider',
['divider'],
['divider' => true]
// Header
['header', $title],
['header' => $title],
['header' => ['title' => $title, ...]] // Remaining options
// Link item
[$name, $url, ...] // Remaining options
['link', $name, $url, ...] // Remaining options
['item' => ['title' => $title, 'url' => $url, ...]] // Remaining options
// Non-link item
['item' => ['title' => $title, ...]] // Remaining options
'My Item'
]
Parameters
- $menu : array<string|int, mixed> = []
-
HTML tags corresponding to menu options (which will be wrapped into
<li>tag). To add separator, pass'divider'. - $options : array<string|int, mixed> = []
-
Attributes for the wrapper (change it with tag).
Return values
stringicon()
Create an icon using the template `icon`.
public
icon(string $icon[, array<string|int, mixed> $options = [] ]) : string
Options
templateVarsProvide template variables for theicontemplate.- Other attributes will be assigned to the wrapper element.
Parameters
- $icon : string
-
Name of the icon.
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —The HTML icon.
link()
{@inheritDoc}
public
link(mixed $title[, mixed $url = null ][, array<string|int, mixed> $options = [] ]) : string
Parameters
- $title : mixed
- $url : mixed = null
- $options : array<string|int, mixed> = []
Return values
stringprogress()
Create a Twitter Bootstrap style progress bar.
public
progress(int|array<string|int, mixed> $widths[, array<string|int, mixed> $options = [] ]) : string
Bar options:
activeIftruethe progress bar will be active. Default isfalse.maxMaximum value for the progress bar. Default is100.minMinimum value for the progress bar. Default is0.stripedIftruethe progress bar will be striped. Default isfalse.typeA string containing thetypeof the progress bar (primary, info, danger, success, warning). Default to'primary'.templateVarsProvide template variables for theprogressBartemplate.- Other attributes will be assigned to the progress bar element.
Parameters
- $widths : int|array<string|int, mixed>
-
Progress width
intThe width (in %) of the bar.arrayAn array of bars, with, for each bar, the following fields:widthrequired The width of the bar.- Other options possible (see above).
- $options : array<string|int, mixed> = []
-
Array of options. See above.
Return values
string —The HTML bootstrap progress bar.
tooltip()
Create a Twitter Bootstrap style tooltip.
public
tooltip(string $text, string $tooltip[, array<string|int, mixed> $options = [] ]) : string
Options
toggleThe 'data-toggle' HTML attribute.placementThedata-placementHTML attribute.tagThe tag to use.templateVarsProvide template variables for thetooltiptemplate.- Other attributes will be assigned to the wrapper element.
Parameters
- $text : string
-
The HTML tag inner text.
- $tooltip : string
-
The tooltip text.
- $options : array<string|int, mixed> = []
-
An array of options. See above. Default values are retrieved from the configuration.
Return values
string —The text wrapped in the specified HTML tag with a tooltip.
_addButtonClasses()
Add classes to options according to the default values of bootstrap-type and bootstrap-size for button (see configuration).
protected
_addButtonClasses(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
-
The initial options with btype and/or size, block values.
Return values
array<string|int, mixed> —The new options with class values (btn, and btn-* according to initial options).
_easyIconOption()
Remove the `easyIcon` option from the given array and return it together with the array.
protected
_easyIconOption(array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
-
Array of options from which the easy-icon option should be extracted.
Return values
array<string|int, mixed> —An array containing the options and the easy-icon option.
_injectIcon()
Inject icon into the given string.
protected
_injectIcon(string $title, bool $easyIcon) : string
Parameters
- $title : string
-
Input string where icon should be injected following the easy-icon process.
- $easyIcon : bool
-
Boolean indicating if the easy-icon process should be applied.
Return values
string_isAssociativeArray()
Check weither the specified array is associative or not.
protected
_isAssociativeArray(array<string|int, mixed> $array) : bool
Parameters
- $array : array<string|int, mixed>
-
The array to check.
Return values
bool —true if the array is associative, false otherwize.
_makeIcon()
Try to convert the specified string to a bootstrap icon. The string is converted if it matches a format `i:icon-name` (leading and trailing spaces or ignored) and if easy-icon is activated.
protected
_makeIcon(string $text[, bool &$converted = false ]) : string
Note: This function will currently fail if the Html helper associated with the view is not BootstrapHtmlHelper.
Parameters
- $text : string
-
The string to convert.
- $converted : bool = false
-
If specified, will contains
trueif the text was converted,falseotherwize.
Return values
string —The text after conversion.