HtmlHelper
class HtmlHelper(View $view, array $settings = [])
The role of the HtmlHelper in CakePHP is to make HTML-related options easier, faster, and more resilient to change. Using this helper will enable your application to be more light on its feet, and more flexible on where it is placed in relation to the root of a domain.
Many HtmlHelper methods include a $options parameter, that allow you to tack on any extra attributes on your tags. Here are a few examples of how to use the $options parameter:
Desired attributes: <tag class="someClass" />
Array parameter: array('class' => 'someClass')
Desired attributes: <tag name="foo" value="bar" />
Array parameter: array('name' => 'foo', 'value' => 'bar')NOTE
The HtmlHelper is available in all views by default. If you're getting an error informing you that it isn't there, it's usually due to its name being missing from a manually configured $helpers controller variable.
Inserting Well-Formatted elements
The most important task the HtmlHelper accomplishes is creating well formed markup. Don't be afraid to use it often - you can cache views in CakePHP in order to save some CPU cycles when views are being rendered and delivered. This section will cover some of the methods of the HtmlHelper and how to use them.
method HtmlHelper::charset($charset=null)
method HtmlHelper::css(mixed $path, array $options = [])
method HtmlHelper::meta(string $type, string $url = null, array $options = [])
method HtmlHelper::docType(string $type = 'xhtml-strict')
method HtmlHelper::style(array $data, boolean $oneline = true)
method HtmlHelper::image(string $path, array $options = [])
method HtmlHelper::link(string $title, mixed $url = null, array $options = [])
method HtmlHelper::media(string|array $path, array $options)
method HtmlHelper::tag(string $tag, string $text, array $options)
method HtmlHelper::div(string $class, string $text, array $options)
method HtmlHelper:😗* para(string $class, string $text, array $options)**()
method HtmlHelper::script(mixed $url, mixed $options)
method HtmlHelper:😗* scriptBlock($code, $options = [])**()
method HtmlHelper::scriptStart($options = [])
method HtmlHelper::scriptEnd()
method HtmlHelper::nestedList(array $list, array $options = [], array $itemOptions = [], string $tag = 'ul')
method HtmlHelper::tableHeaders(array $names, array $trOptions = null, array $thOptions = null)
method HtmlHelper::tableCells(array $data, array $oddTrOptions = null, array $evenTrOptions = null, $useCount = false, $continueOddEven = true)
method HtmlHelper::url(mixed $url = NULL, boolean $full = false)
method HtmlHelper::useTag(string $tag)
HTML5 Tags
Since v2.12.11, HtmlHelper outputs the script, link and style tags of HTML5, without the type attribute and without CDATA:
| Call | Output |
|---|---|
$this->Html->script('app') | <script src="/js/app.js"></script> |
$this->Html->css('style') | <link rel="stylesheet" href="/css/style.css"/> |
$this->Html->css('style', ['rel' => 'import']) | <style>@import url(/css/style.css);</style> |
$this->Html->scriptBlock('init();') | <script>init();</script> |
Set the html5 setting of the helper to false for the output of XHTML, with type="text/javascript", type="text/css" and the script blocks in CDATA, for example for pages served as application/xhtml+xml:
public $helpers = ['Html' => ['html5' => false]];The safe option of scriptBlock() and scriptStart() wraps the script in CDATA when it is true; its default is false, or true with html5 set to false. JsHelper follows the same setting.
Changing the tags output by HtmlHelper
method HtmlHelper::loadConfig(mixed $configFile, string $path = null)
Creating breadcrumb trails with HtmlHelper
method HtmlHelper::getCrumbs(string $separator = '»', string|array|bool $startText = false)
method HtmlHelper::addCrumb(string $name, string $link = null, mixed $options = null)
method HtmlHelper::getCrumbList(array $options = [], mixed $startText)
