Html Helper class for easy use of HTML widgets.
HtmlHelper encloses all methods needed while working with HTML pages.
Cake\View\Helper\UrlHelper
Cake\View\View
The View instance this helper is attached to
array<string, mixed>
Runtime config
bool
Whether the config property has already been configured with defaults
array<string, mixed>
Default config for this class
array<string, array>
A helper lookup table used to lazy load helper objects.
array<string, array>
Names of script & css files that have been included once
array<string, mixed>
Options for the currently opened script block buffer if any.
Cake\View\StringTemplate|null
StringTemplate instance.
array
List of helpers used by this helper
Provide non fatal errors on missing method calls.
Default Constructor
Returns an array that can be used to describe the internal state of this object.
Lazy loads helpers.
Deletes a single config key.
Reads a config key.
Writes a config key.
Returns a string to be used as onclick handler for confirm dialogs.
Internal function to build a nested list (UL/OL) out of an associative array.
Renders cells for a row of a table.
Adds the given class to the element options
Returns a charset META-tag.
Merge provided config with existing config. Unlike config()
which does a recursive merge for nested keys, this method does a simple merge.
Creates a link element for CSS stylesheets.
Returns a formatted DIV tag for HTML FORMs.
Formats a template string with $data
Returns the config.
Returns the config for this specific key.
Gets templates to use or a specific template.
Get the view instance this helper is bound to.
Creates a formatted IMG element.
Event listeners.
Constructor hook method.
Creates an HTML link.
Creates an HTML link from route path string.
Returns an audio/video element
Creates a link to an external resource and handles basic meta tags
Build a nested list (UL/OL) out of an associative array.
Returns a formatted P tag.
Returns one or many <script>
tags depending on the number of scripts given.
Wrap $script in a script tag.
End a Buffered section of JavaScript capturing. Generates a script tag inline or appends to specified view block depending on the settings used when the scriptBlock was started
Begin a script block that captures output until HtmlHelper::scriptEnd() is called. This capturing block will capture all output between the methods and create a scriptBlock from it.
Sets the config.
Sets templates to use.
Builds CSS style data from an array of CSS properties
Renders a single table cell (A TD with attributes).
Returns a formatted string of table rows (TR's with TD's in them).
Returns a row of formatted and named TABLE headers.
Renders a single table row (A TR with attributes).
Returns a formatted block tag, i.e DIV, SPAN, P.
Returns the templater instance.
__call(string $method, array $params): mixed|void
Provide non fatal errors on missing method calls.
string
$method Method to invoke
array
$params Array of params for the method.
mixed|void
__construct(Cake\View\View $view, array<string, mixed> $config = [])
Default Constructor
Cake\View\View
$view The View this helper is being attached to.
array<string, mixed>
$config optional Configuration settings for the helper.
__debugInfo(): array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
array<string, mixed>
__get(string $name): Cake\View\Helper|null|void
Lazy loads helpers.
string
$name Name of the property being accessed.
Cake\View\Helper|null|void
_configDelete(string $key): void
Deletes a single config key.
string
$key Key to delete.
void
Cake\Core\Exception\CakeException
_configRead(string|null $key): mixed
Reads a config key.
string|null
$key Key to read.
mixed
_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
array<string, mixed>|string
$key Key to write to.
mixed
$value Value to write.
string|bool
$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
void
Cake\Core\Exception\CakeException
_confirm(string $okCode, string $cancelCode): string
Returns a string to be used as onclick handler for confirm dialogs.
string
$okCode Code to be executed after user chose 'OK'
string
$cancelCode Code to be executed after user chose 'Cancel'
string
_nestedListItem(array $items, array<string, mixed> $options, array<string, mixed> $itemOptions): string
Internal function to build a nested list (UL/OL) out of an associative array.
array
$items Set of elements to list.
array<string, mixed>
$options Additional HTML attributes of the list (ol/ul) tag.
array<string, mixed>
$itemOptions Options and additional HTML attributes of the list item (LI) tag.
string
_renderCells(array $line, bool $useCount = false): array<string>
Renders cells for a row of a table.
This is a helper method for tableCells(). Overload this method as you need to change the behavior of the cell rendering.
array
$line Line data to render.
bool
$useCount optional Renders the count into the row. Default is false.
array<string>
addClass(array<string, mixed> $options, string $class, string $key = 'class'): array<string, mixed>
Adds the given class to the element options
array<string, mixed>
$options Array options/attributes to add a class to
string
$class The class name being added.
string
$key optional the key to use for class. Defaults to 'class'
.
array<string, mixed>
charset(string|null $charset = null): string
Returns a charset META-tag.
string|null
$charset optional The character set to be used in the meta tag. If empty, The App.encoding value will be used. Example: "utf-8".
string
configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config()
which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string
$key The key to set, or a complete array of configs.
mixed|null
$value optional The value to set.
$this
css(array<string>|string $path, array<string, mixed> $options = []): string|null
Creates a link element for CSS stylesheets.
Include one CSS file:
echo $this->Html->css('styles.css');
Include multiple CSS files:
echo $this->Html->css(['one.css', 'two.css']);
Add the stylesheet to view block "css":
$this->Html->css('styles.css', ['block' => true]);
Add the stylesheet to a custom block:
$this->Html->css('styles.css', ['block' => 'layoutCss']);
block
Set to true to append output to view block "css" or provide custom block name.once
Whether the css file should be checked for uniqueness. If true css files will only be included once, use false to allow the same css to be included more than once per request.plugin
False value will prevent parsing path as a pluginrel
Defaults to 'stylesheet'. If equal to 'import' the stylesheet will be imported.fullBase
If true the URL will get a full address for the css file.All other options will be treated as HTML attributes. If the request contains a cspStyleNonce
attribute, that value will be applied as the nonce
attribute on the generated HTML.
array<string>|string
$path The name of a CSS style sheet or an array containing names of CSS stylesheets. If $path
is prefixed with '/', the path will be relative to the webroot of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
array<string, mixed>
$options optional Array of options and HTML arguments.
string|null
div(string|null $class = null, string|null $text = null, array<string, mixed> $options = []): string
Returns a formatted DIV tag for HTML FORMs.
escape
Whether the contents should be html_entity escaped.string|null
$class optional CSS class name of the div element.
string|null
$text optional String content that will appear inside the div element. If null, only a start tag will be printed
array<string, mixed>
$options optional Additional HTML attributes of the DIV tag
string
formatTemplate(string $name, array<string, mixed> $data): string
Formats a template string with $data
string
$name The template name.
array<string, mixed>
$data The data to insert.
string
getConfig(string|null $key = null, mixed $default = null): mixed
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
string|null
$key optional The key to get or null for the whole config.
mixed
$default optional The return value when the key does not exist.
mixed
getConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
string
$key The key to get.
mixed
InvalidArgumentException
getTemplates(string|null $template = null): array|string
Gets templates to use or a specific template.
string|null
$template optional String for reading a specific template, null for all.
array|string
getView(): Cake\View\View
Get the view instance this helper is bound to.
Cake\View\View
image(array|string $path, array<string, mixed> $options = []): string
Creates a formatted IMG element.
This method will set an empty alt attribute if one is not supplied.
Create a regular image:
echo $this->Html->image('cake_icon.png', ['alt' => 'CakePHP']);
Create an image link:
echo $this->Html->image('cake_icon.png', ['alt' => 'CakePHP', 'url' => 'https://cakephp.org']);
url
If provided an image link will be generated and the link will point at $options['url']
.fullBase
If true the src attribute will get a full address for the image file.plugin
False value will prevent parsing path as a pluginarray|string
$path Path to the image file, relative to the webroot/img/ directory.
array<string, mixed>
$options optional Array of HTML attributes. See above for special options.
string
implementedEvents(): array<string, mixed>
Event listeners.
By defining one of the callback methods a helper is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want helpers to listen to non-standard events.
array<string, mixed>
initialize(array<string, mixed> $config): void
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
array<string, mixed>
$config The configuration settings provided to this helper.
void
link(array|string $title, array|string|null $url = null, array<string, mixed> $options = []): string
Creates an HTML link.
If $url starts with "http://" this is treated as an external link. Else, it is treated as a path to controller/action and parsed with the UrlHelper::build() method.
If the $url is empty, $title is used instead.
escape
Set to false to disable escaping of title and attributes.escapeTitle
Set to false to disable escaping of title. Takes precedence over value of escape
)confirm
JavaScript confirmation message.array|string
$title The content to be wrapped by <a>
tags. Can be an array if $url is null. If $url is null, $title will be used as both the URL and title.
array|string|null
$url optional Cake-relative URL or array of URL parameters, or external URL (starts with http://)
array<string, mixed>
$options optional Array of options and HTML attributes.
string
linkFromPath(string $title, string $path, array $params = [], array<string, mixed> $options = []): string
Creates an HTML link from route path string.
escape
Set to false to disable escaping of title and attributes.escapeTitle
Set to false to disable escaping of title. Takes precedence over value of escape
)confirm
JavaScript confirmation message.string
$title The content to be wrapped by <a>
tags.
string
$path Cake-relative route path.
array
$params optional An array specifying any additional parameters. Can be also any special parameters supported by Router::url()
.
array<string, mixed>
$options optional Array of options and HTML attributes.
string
media(array|string $path, array<string, mixed> $options = []): string
Returns an audio/video element
Using an audio file:
echo $this->Html->media('audio.mp3', ['fullBase' => true]);
Outputs:
<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>
Using a video file:
echo $this->Html->media('video.mp4', ['text' => 'Fallback text']);
Outputs:
<video src="/files/video.mp4">Fallback text</video>
Using multiple video files:
echo $this->Html->media( ['video.mp4', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]], ['tag' => 'video', 'autoplay'] );
Outputs:
<video autoplay="autoplay"> <source src="/files/video.mp4" type="video/mp4"/> <source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/> </video>
tag
Type of media element to generate, either "audio" or "video". If tag is not provided it's guessed based on file's mime type.text
Text to include inside the audio/video tagpathPrefix
Path prefix to use for relative URLs, defaults to 'files/'fullBase
If provided the src attribute will get a full address including domain namearray|string
$path Path to the video file, relative to the webroot/{$options['pathPrefix']} directory. Or an array where each item itself can be a path string or an associate array containing keys src
and type
array<string, mixed>
$options optional Array of HTML attributes, and special options above.
string
meta(array<string, mixed>|string $type, array|string|null $content = null, array<string, mixed> $options = []): string|null
Creates a link to an external resource and handles basic meta tags
Create a meta tag that is output inline:
$this->Html->meta('icon', 'favicon.ico');
Append the meta tag to custom view block "meta":
$this->Html->meta('description', 'A great page', ['block' => true]);
Append the meta tag to custom view block:
$this->Html->meta('description', 'A great page', ['block' => 'metaTags']);
Create a custom meta tag:
$this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);
block
- Set to true to append output to view block "meta" or provide custom block name.array<string, mixed>|string
$type The title of the external resource, Or an array of attributes for a custom meta tag.
array|string|null
$content optional The address of the external resource or string for content attribute
array<string, mixed>
$options optional Other attributes for the generated tag. If the type attribute is html, rss, atom, or icon, the mime-type is returned.
string|null
nestedList(array $list, array<string, mixed> $options = [], array<string, mixed> $itemOptions = []): string
Build a nested list (UL/OL) out of an associative array.
Options for $options:
tag
- Type of list tag to use (ol/ul)Options for $itemOptions:
even
- Class to use for even rows.odd
- Class to use for odd rows.array
$list Set of elements to list
array<string, mixed>
$options optional Options and additional HTML attributes of the list (ol/ul) tag.
array<string, mixed>
$itemOptions optional Options and additional HTML attributes of the list item (LI) tag.
string
para(string|null $class, string|null $text, array<string, mixed> $options = []): string
Returns a formatted P tag.
escape
Whether the contents should be html_entity escaped.string|null
$class CSS class name of the p element.
string|null
$text String content that will appear inside the p element.
array<string, mixed>
$options optional Additional HTML attributes of the P tag
string
script(array<string>|string $url, array<string, mixed> $options = []): string|null
Returns one or many <script>
tags depending on the number of scripts given.
If the filename is prefixed with "/", the path will be relative to the base path of your application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
Include one script file:
echo $this->Html->script('styles.js');
Include multiple script files:
echo $this->Html->script(['one.js', 'two.js']);
Add the script file to a custom block:
$this->Html->script('styles.js', ['block' => 'bodyScript']);
block
Set to true to append output to view block "script" or provide custom block name.once
Whether the script should be checked for uniqueness. If true scripts will only be included once, use false to allow the same script to be included more than once per request.plugin
False value will prevent parsing path as a pluginfullBase
If true the url will get a full address for the script file.All other options will be added as attributes to the generated script tag. If the current request has a cspScriptNonce
attribute, that value will be inserted as a nonce
attribute on the script tag.
array<string>|string
$url String or array of javascript files to include
array<string, mixed>
$options optional Array of options, and html attributes see above.
string|null
scriptBlock(string $script, array<string, mixed> $options = []): string|null
Wrap $script in a script tag.
block
Set to true to append output to view block "script" or provide custom block name.string
$script The script to wrap
array<string, mixed>
$options optional The options to use. Options not listed above will be treated as HTML attributes.
string|null
scriptEnd(): string|null
End a Buffered section of JavaScript capturing. Generates a script tag inline or appends to specified view block depending on the settings used when the scriptBlock was started
string|null
scriptStart(array<string, mixed> $options = []): void
Begin a script block that captures output until HtmlHelper::scriptEnd() is called. This capturing block will capture all output between the methods and create a scriptBlock from it.
block
Set to true to append output to view block "script" or provide custom block name.array<string, mixed>
$options optional Options for the code block.
void
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
Sets the config.
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
array<string, mixed>|string
$key The key to set, or a complete array of configs.
mixed|null
$value optional The value to set.
bool
$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
$this
Cake\Core\Exception\CakeException
setTemplates(array<string> $templates): $this
Sets templates to use.
array<string>
$templates Templates to be added.
$this
style(array<string, string> $data, bool $oneLine = true): string
Builds CSS style data from an array of CSS properties
echo $this->Html->style(['margin' => '10px', 'padding' => '10px'], true); // creates 'margin:10px;padding:10px;'
array<string, string>
$data Style data array, keys will be used as property names, values as property values.
bool
$oneLine optional Whether the style block should be displayed on one line.
string
tableCell(string $content, array<string, mixed> $options = []): string
Renders a single table cell (A TD with attributes).
string
$content The content of the cell.
array<string, mixed>
$options optional HTML attributes.
string
tableCells(array|string $data, array<string, mixed>|bool|null $oddTrOptions = null, array<string, mixed>|bool|null $evenTrOptions = null, bool $useCount = false, bool $continueOddEven = true): string
Returns a formatted string of table rows (TR's with TD's in them).
array|string
$data Array of table data
array<string, mixed>|bool|null
$oddTrOptions optional HTML options for odd TR elements if true useCount is used
array<string, mixed>|bool|null
$evenTrOptions optional HTML options for even TR elements
bool
$useCount optional adds class "column-$i"
bool
$continueOddEven optional If false, will use a non-static $count variable, so that the odd/even count is reset to zero just for that call.
string
tableHeaders(array $names, array<string, mixed>|null $trOptions = null, array<string, mixed>|null $thOptions = null): string
Returns a row of formatted and named TABLE headers.
array
$names Array of tablenames. Each tablename can be string, or array with name and an array with a set of attributes to its specific tag
array<string, mixed>|null
$trOptions optional HTML options for TR elements.
array<string, mixed>|null
$thOptions optional HTML options for TH elements.
string
tableRow(string $content, array<string, mixed> $options = []): string
Renders a single table row (A TR with attributes).
string
$content The content of the row.
array<string, mixed>
$options optional HTML attributes.
string
tag(string $name, string|null $text = null, array<string, mixed> $options = []): string
Returns a formatted block tag, i.e DIV, SPAN, P.
escape
Whether the contents should be html_entity escaped.string
$name Tag name.
string|null
$text optional String content that will appear inside the HTML element. If null, only a start tag will be printed
array<string, mixed>
$options optional Additional HTML attributes of the HTML tag, see above.
string
templater(): Cake\View\StringTemplate
Returns the templater instance.
Cake\View\StringTemplate
Cake\View\Helper\UrlHelper
The View instance this helper is attached to
Cake\View\View
Runtime config
array<string, mixed>
Whether the config property has already been configured with defaults
bool
Default config for this class
array<string, mixed>
A helper lookup table used to lazy load helper objects.
array<string, array>
Names of script & css files that have been included once
array<string, array>
Options for the currently opened script block buffer if any.
array<string, mixed>
StringTemplate instance.
Cake\View\StringTemplate|null
List of helpers used by this helper
array
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.View.Helper.HtmlHelper.html