Class HtmlHelper
Html Helper class for easy use of HTML widgets.
HtmlHelper encloses all methods needed while working with HTML pages.
- Cake\View\Helper implements Cake\Event\EventListenerInterface uses Cake\Core\InstanceConfigTrait
-
Cake\View\Helper\HtmlHelper uses Cake\View\StringTemplateTrait
Link: http://book.cakephp.org/3.0/en/views/helpers/html.html
Located at View/Helper/HtmlHelper.php
Method Detail
__constructsource public
__construct( Cake\View\View $View , array $config [] )
Constructor
Settings
-
templatesEither a filename to a config containing templates. Or an array of templates to load. See Cake\View\StringTemplate for template formatting.
Customizing tag sets
Using the templates option you can redefine the tag HtmlHelper will use.
Parameters
-
Cake\View\View$View - The View this helper is being attached to.
- array
$configoptional [] - Configuration settings for the helper.
Overrides
Cake\View\Helper::__construct() _nestedListItemsource protected
_nestedListItem( array $items , array $options , array $itemOptions )
Internal function to build a nested list (UL/OL) out of an associative array.
Parameters
- array
$items - Set of elements to list.
- array
$options - Additional HTML attributes of the list (ol/ul) tag.
- array
$itemOptions - Options and additional HTML attributes of the list item (LI) tag.
Returns
stringThe nested list element
See
Cake\View\Helper\HtmlHelper::nestedList()_prepareCrumbssource protected
_prepareCrumbs( string|array|boolean $startText , boolean $escape true )
Prepends startText to crumbs array if set
Parameters
- string|array|boolean
$startText - Text to prepend
- boolean
$escapeoptional true - If the output should be escaped or not
Returns
arrayCrumb list including startText (if provided)
addCrumbsource public
addCrumb( string $name , string|null $link null , array $options [] )
Adds a link to the breadcrumbs array.
Parameters
- string
$name - Text for link
- string|null
$linkoptional null - URL for link (if empty it won't be a link)
- array
$optionsoptional [] - Link attributes e.g. ['id' => 'selected']
Returns
mixed$this
See
HtmlHelper::link() for details on $options that can be used.Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-breadcrumb-trails-with-htmlhelpercharsetsource public
charset( string|null $charset null )
Returns a charset META-tag.
Parameters
- string|null
$charsetoptional null - The character set to be used in the meta tag. If empty, The App.encoding value will be used. Example: "utf-8".
Returns
stringA meta tag containing the specified character set.
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-charset-tagscsssource public
css( string|array $path , array $options [] )
Creates a link element for CSS stylesheets.
Usage
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']);
Options
-
blockSet to true to append output to view block "css" or provide custom block name. -
onceWhether or not 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. -
pluginFalse value will prevent parsing path as a plugin -
relDefaults to 'stylesheet'. If equal to 'import' the stylesheet will be imported. -
fullBaseIf true the URL will get a full address for the css file.
Parameters
- string|array
$path - The name of a CSS style sheet or an array containing names of CSS stylesheets. If
$pathis 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
$optionsoptional [] - Array of options and HTML arguments.
Returns
string|nullCSS <link /> or <style /> tag, depending on the type of link.
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#linking-to-css-filesdivsource public
div( string $class null , string $text null , array $options [] )
Returns a formatted DIV tag for HTML FORMs.
Options
-
escapeWhether or not the contents should be html_entity escaped.
Parameters
- string
$classoptional null - CSS class name of the div element.
- string
$textoptional null - String content that will appear inside the div element. If null, only a start tag will be printed
- array
$optionsoptional [] - Additional HTML attributes of the DIV tag
Returns
stringThe formatted DIV element
docTypesource public
docType( string $type 'html5' )
Returns a doctype string.
Possible doctypes:
- html4-strict: HTML4 Strict. - html4-trans: HTML4 Transitional. - html4-frame: HTML4 Frameset. - html5: HTML5. Default value. - xhtml-strict: XHTML1 Strict. - xhtml-trans: XHTML1 Transitional. - xhtml-frame: XHTML1 Frameset. - xhtml11: XHTML1.1.
Parameters
- string
$typeoptional 'html5' - Doctype to use.
Returns
string|nullDoctype string
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-doctype-tagsgetCrumbListsource public
getCrumbList( array $options [] , string|array|boolean $startText false )
Returns breadcrumbs as a (x)html list
This method uses HtmlHelper::tag() to generate list and its elements. Works similar to HtmlHelper::getCrumbs(), so it uses options which every crumb was added with.
Options
-
separatorSeparator content to insert in between breadcrumbs, defaults to '' -
firstClassClass for wrapper tag on the first breadcrumb, defaults to 'first' -
lastClassClass for wrapper tag on current active page, defaults to 'last'
Parameters
- array
$optionsoptional [] - Array of HTML attributes to apply to the generated list elements.
- string|array|boolean
$startTextoptional false - This will be the first crumb, if false it defaults to first crumb in array. Can also be an array, see
HtmlHelper::getCrumbsfor details.
Returns
string|nullBreadcrumbs HTML list.
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-breadcrumb-trails-with-htmlhelpergetCrumbssource public
getCrumbs( string $separator '»' , string|array|boolean $startText false )
Returns the breadcrumb trail as a sequence of »-separated links.
If $startText is an array, the accepted keys are:
-
textDefine the text/content for the link. -
urlDefine the target of the created link.
All other keys will be passed to HtmlHelper::link() as the $options parameter.
Parameters
- string
$separatoroptional '»' - Text to separate crumbs.
- string|array|boolean
$startTextoptional false - This will be the first crumb, if false it defaults to first crumb in array. Can also be an array, see above for details.
Returns
string|nullComposed bread crumbs
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-breadcrumb-trails-with-htmlhelperimagesource public
image( string $path , array $options [] )
Creates a formatted IMG element.
This method will set an empty alt attribute if one is not supplied.
Usage:
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' => 'http://cakephp.org']);
Options:
-
urlIf provided an image link will be generated and the link will point at$options['url']. -
fullBaseIf true the src attribute will get a full address for the image file. -
pluginFalse value will prevent parsing path as a plugin
Parameters
- string
$path - Path to the image file, relative to the app/webroot/img/ directory.
- array
$optionsoptional [] - Array of HTML attributes. See above for special options.
Returns
stringcompleted img tag
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#linking-to-imagesimplementedEventssource public
implementedEvents( )
Event listeners.
Returns
arrayarray
Overrides
Cake\View\Helper::implementedEvents() linksource public
link( string $title , string|array|null $url null , array $options [] )
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.
Options
-
escapeSet to false to disable escaping of title and attributes. -
escapeTitleSet to false to disable escaping of title. Takes precedence over value ofescape) -
confirmJavaScript confirmation message.
Parameters
- string
$title - The content to be wrapped by <a> tags.
- string|array|null
$urloptional null - Cake-relative URL or array of URL parameters, or external URL (starts with http://)
- array
$optionsoptional [] - Array of options and HTML attributes.
Returns
stringAn
<a /> element.Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-linksmediasource public
media( string|array $path , array $options [] )
Returns an audio/video element
Usage
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>
Options
-
tagType of media element to generate, either "audio" or "video". If tag is not provided it's guessed based on file's mime type. -
textText to include inside the audio/video tag -
pathPrefixPath prefix to use for relative URLs, defaults to 'files/' -
fullBaseIf provided the src attribute will get a full address including domain name
Parameters
- string|array
$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
srcandtype - array
$optionsoptional [] - Array of HTML attributes, and special options above.
Returns
stringGenerated media element
metasource public
meta( string|array $type , string|array|null $content null , array $options [] )
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']);
Options
-
block- Set to true to append output to view block "meta" or provide custom block name.
Parameters
- string|array
$type - The title of the external resource
- string|array|null
$contentoptional null - The address of the external resource or string for content attribute
- array
$optionsoptional [] - Other attributes for the generated tag. If the type attribute is html, rss, atom, or icon, the mime-type is returned.
Returns
stringA completed
<link /> element.Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-meta-tagsnestedListsource public
nestedList( array $list , array $options [] , array $itemOptions [] )
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.
Parameters
- array
$list - Set of elements to list
- array
$optionsoptional [] - Options and additional HTML attributes of the list (ol/ul) tag.
- array
$itemOptionsoptional [] - Options and additional HTML attributes of the list item (LI) tag.
Returns
stringThe nested list
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-nested-listsparasource public
para( string $class , string $text , array $options [] )
Returns a formatted P tag.
Options
-
escapeWhether or not the contents should be html_entity escaped.
Parameters
- string
$class - CSS class name of the p element.
- string
$text - String content that will appear inside the p element.
- array
$optionsoptional [] - Additional HTML attributes of the P tag
Returns
stringThe formatted P element
scriptsource public
script( string|array $url , array $options [] )
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.
Usage
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']);
Options
-
blockSet to true to append output to view block "script" or provide custom block name. -
onceWhether or not 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. -
pluginFalse value will prevent parsing path as a plugin -
fullBaseIf true the url will get a full address for the script file.
Parameters
- string|array
$url - String or array of javascript files to include
- array
$optionsoptional [] - Array of options, and html attributes see above.
Returns
string|nullString of
<script /> tags or null if block is specified in options or if $once is true and the file has been included before.Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#linking-to-javascript-filesscriptBlocksource public
scriptBlock( string $script , array $options [] )
Wrap $script in a script tag.
Options
-
safe(boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]> -
blockSet to true to append output to view block "script" or provide custom block name.
Parameters
- string
$script - The script to wrap
- array
$optionsoptional [] - The options to use. Options not listed above will be treated as HTML attributes.
Returns
string|nullString or null depending on the value of
$options['block']Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-inline-javascript-blocksscriptEndsource public
scriptEnd( )
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
Returns
string|nullDepending on the settings of scriptStart() either a script tag or null
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-javascript-blocksscriptStartsource public
scriptStart( array $options [] )
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.
Options
-
safeWhether the code block should contain a CDATA -
blockSet to true to append output to view block "script" or provide custom block name.
Parameters
- array
$optionsoptional [] - Options for the code block.
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-javascript-blocksstylesource public
style( array $data , boolean $oneLine true )
Builds CSS style data from an array of CSS properties
Usage:
echo $this->Html->style(['margin' => '10px', 'padding' => '10px'], true); // creates 'margin:10px;padding:10px;'
Parameters
- array
$data - Style data array, keys will be used as property names, values as property values.
- boolean
$oneLineoptional true - Whether or not the style block should be displayed on one line.
Returns
stringCSS styling data
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-css-programaticallytableCellssource public
tableCells( array|string $data , array|boolean|null $oddTrOptions null , array|boolean|null $evenTrOptions null , boolean $useCount false , boolean $continueOddEven true )
Returns a formatted string of table rows (TR's with TD's in them).
Parameters
- array|string
$data - Array of table data
- array|boolean|null
$oddTrOptionsoptional null - HTML options for odd TR elements if true useCount is used
- array|boolean|null
$evenTrOptionsoptional null - HTML options for even TR elements
- boolean
$useCountoptional false - adds class "column-$i"
- boolean
$continueOddEvenoptional true - If false, will use a non-static $count variable, so that the odd/even count is reset to zero just for that call.
Returns
stringFormatted HTML
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-table-cellstableHeaderssource public
tableHeaders( array $names , array $trOptions null , array $thOptions null )
Returns a row of formatted and named TABLE headers.
Parameters
- array
$names - Array of tablenames. Each tablename also can be a key that points to an array with a set of attributes to its specific tag
- array
$trOptionsoptional null - HTML options for TR elements.
- array
$thOptionsoptional null - HTML options for TH elements.
Returns
stringCompleted table headers
Link
http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-table-headingstagsource public
tag( string $name , string $text null , array $options [] )
Returns a formatted block tag, i.e DIV, SPAN, P.
Options
-
escapeWhether or not the contents should be html_entity escaped.
Parameters
- string
$name - Tag name.
- string
$textoptional null - String content that will appear inside the div element. If null, only a start tag will be printed
- array
$optionsoptional [] - Additional HTML attributes of the DIV tag, see above.
Returns
stringThe formatted tag element
Methods inherited from Cake\View\Helper
__callsource public
__call( string $method , array $params )
Provide non fatal errors on missing method calls.
Parameters
- string
$method - Method to invoke
- array
$params - Array of params for the method.
__debugInfosource public
__debugInfo( )
Returns an array that can be used to describe the internal state of this object.
Returns
arrayarray
__getsource public
__get( string $name )
Lazy loads helpers.
Parameters
- string
$name - Name of the property being accessed.
Returns
Cake\View\Helper|nullHelper instance if helper with provided name exists
_confirmsource protected
_confirm( string $message , string $okCode , string $cancelCode '' , array $options [] )
Returns a string to be used as onclick handler for confirm dialogs.
Parameters
- string
$message - Message to be displayed
- string
$okCode - Code to be executed after user chose 'OK'
- string
$cancelCodeoptional '' - Code to be executed after user chose 'Cancel'
- array
$optionsoptional [] - Array of options
Returns
stringonclick JS code
addClasssource public
addClass( array $options [] , string $class null , string $key 'class' )
Adds the given class to the element options
Parameters
- array
$optionsoptional [] - Array options/attributes to add a class to
- string
$classoptional null - The class name being added.
- string
$keyoptional 'class' - the key to use for class.
Returns
arrayArray of options with $key set.
Methods used from Cake\View\StringTemplateTrait
formatTemplatesource public
formatTemplate( string $name , array $data )
Format a template string with $data
Parameters
- string
$name - The template name.
- array
$data - The data to insert.
Returns
stringstring
templatersource public
templater( )
templater
Returns
Cake\View\StringTemplate\Cake\View\StringTemplate
templatessource public
templates( string|null|array $templates null )
Get/set templates to use.
Parameters
- string|null|array
$templatesoptional null - null or string allow reading templates. An array allows templates to be added.
Returns
mixed$this|string|array
Methods used from Cake\Core\InstanceConfigTrait
_configDeletesource protected
_configDelete( string $key )
Delete a single config key
Parameters
- string
$key - Key to delete.
Throws
Cake\Core\Exception\Exceptionif attempting to clobber existing config
_configReadsource protected
_configRead( string|null $key )
Read a config variable
Parameters
- string|null
$key - Key to read.
Returns
mixedmixed
_configWritesource protected
_configWrite( string|array $key , mixed $value , boolean|string $merge false )
Write a config variable
Parameters
- string|array
$key - Key to write to.
- mixed
$value - Value to write.
- boolean|string
$mergeoptional false - True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Throws
Cake\Core\Exception\Exceptionif attempting to clobber existing config
configsource public
config( string|array|null $key null , mixed|null $value null , boolean $merge true )
Usage
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
- string|array|null
$keyoptional null - The key to get/set, or a complete array of configs.
- mixed|null
$valueoptional null - The value to set.
- boolean
$mergeoptional true - Whether to recursively merge or overwrite existing config, defaults to true.
Returns
mixedConfig value being read, or the object itself on write operations.
Throws
Cake\Core\Exception\ExceptionWhen trying to set a key that is invalid.
configShallowsource public
configShallow( string|array $key , mixed|null $value null )
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->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
- string|array
$key - The key to set, or a complete array of configs.
- mixed|null
$valueoptional null - The value to set.
Returns
mixed$this The object itself.
Properties summary
Properties inherited from Cake\View\Helper
$fieldsetsource
public array
Holds the fields ['field_name' => ['type' => 'string', 'length' => 100]], primaryKey and validates ['field_name']
[]
Properties used from Cake\Core\InstanceConfigTrait
$_configInitializedsource
protected boolean
Whether the config property has already been configured with defaults
false Properties used from Cake\View\StringTemplateTrait
Magic properties summary
$Urlsource
public Cake\View\Helper\UrlHelper
© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.View.Helper.HtmlHelper.html