',
'javascriptblock' => '',
'javascriptstart' => '',
'javascriptend' => '',
'confirmJs' => '{{confirm}}',
],
];
/**
* Names of script & css files that have been included once
*
* @var array
*/
protected $_includedAssets = [];
/**
* Options for the currently opened script block buffer if any.
*
* @var array
*/
protected $_scriptBlockOptions = [];
/**
* 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.
*
* @param string|array $type The title of the external resource, Or an array of attributes for a
* custom meta tag.
* @param string|array|null $content The address of the external resource or string for content attribute
* @param array $options Other attributes for the generated tag. If the type attribute is html,
* rss, atom, or icon, the mime-type is returned.
* @return string|null A completed `` element, or null if the element was sent to a block.
* @link https://book.cakephp.org/4/en/views/helpers/html.html#creating-meta-tags
*/
public function meta($type, $content = null, array $options = []): ?string
{
if (!is_array($type)) {
$types = [
'rss' => ['type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $content],
'atom' => ['type' => 'application/atom+xml', 'title' => $type, 'link' => $content],
'icon' => ['type' => 'image/x-icon', 'rel' => 'icon', 'link' => $content],
'keywords' => ['name' => 'keywords', 'content' => $content],
'description' => ['name' => 'description', 'content' => $content],
'robots' => ['name' => 'robots', 'content' => $content],
'viewport' => ['name' => 'viewport', 'content' => $content],
'canonical' => ['rel' => 'canonical', 'link' => $content],
'next' => ['rel' => 'next', 'link' => $content],
'prev' => ['rel' => 'prev', 'link' => $content],
'first' => ['rel' => 'first', 'link' => $content],
'last' => ['rel' => 'last', 'link' => $content],
];
if ($type === 'icon' && $content === null) {
$types['icon']['link'] = 'favicon.ico';
}
if (isset($types[$type])) {
$type = $types[$type];
} elseif (!isset($options['type']) && $content !== null) {
if (is_array($content) && isset($content['_ext'])) {
$type = $types[$content['_ext']];
} else {
$type = ['name' => $type, 'content' => $content];
}
} elseif (isset($options['type'], $types[$options['type']])) {
$type = $types[$options['type']];
unset($options['type']);
} else {
$type = [];
}
}
$options += $type + ['block' => null];
$out = '';
if (isset($options['link'])) {
if (is_array($options['link'])) {
$options['link'] = $this->Url->build($options['link']);
} else {
$options['link'] = $this->Url->assetUrl($options['link']);
}
if (isset($options['rel']) && $options['rel'] === 'icon') {
$out = $this->formatTemplate('metalink', [
'url' => $options['link'],
'attrs' => $this->templater()->formatAttributes($options, ['block', 'link']),
]);
$options['rel'] = 'shortcut icon';
}
$out .= $this->formatTemplate('metalink', [
'url' => $options['link'],
'attrs' => $this->templater()->formatAttributes($options, ['block', 'link']),
]);
} else {
$out = $this->formatTemplate('meta', [
'attrs' => $this->templater()->formatAttributes($options, ['block', 'type']),
]);
}
if (empty($options['block'])) {
return $out;
}
if ($options['block'] === true) {
$options['block'] = __FUNCTION__;
}
$this->_View->append($options['block'], $out);
return null;
}
/**
* Returns a charset META-tag.
*
* @param string|null $charset The character set to be used in the meta tag. If empty,
* The App.encoding value will be used. Example: "utf-8".
* @return string A meta tag containing the specified character set.
* @link https://book.cakephp.org/4/en/views/helpers/html.html#creating-charset-tags
*/
public function charset(?string $charset = null): string
{
if (empty($charset)) {
$charset = strtolower((string)Configure::read('App.encoding'));
}
return $this->formatTemplate('charset', [
'charset' => !empty($charset) ? $charset : 'utf-8',
]);
}
/**
* 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
*
* - `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.
*
* @param string|array $title The content to be wrapped by `` tags.
* Can be an array if $url is null. If $url is null, $title will be used as both the URL and title.
* @param string|array|null $url Cake-relative URL or array of URL parameters, or
* external URL (starts with http://)
* @param array $options Array of options and HTML attributes.
* @return string An `` element.
* @link https://book.cakephp.org/4/en/views/helpers/html.html#creating-links
*/
public function link($title, $url = null, array $options = []): string
{
$escapeTitle = true;
if ($url !== null) {
$url = $this->Url->build($url, $options);
unset($options['fullBase']);
} else {
$url = $this->Url->build($title);
$title = htmlspecialchars_decode($url, ENT_QUOTES);
$title = h(urldecode($title));
$escapeTitle = false;
}
if (isset($options['escapeTitle'])) {
$escapeTitle = $options['escapeTitle'];
unset($options['escapeTitle']);
} elseif (isset($options['escape'])) {
$escapeTitle = $options['escape'];
}
if ($escapeTitle === true) {
$title = h($title);
} elseif (is_string($escapeTitle)) {
/** @psalm-suppress PossiblyInvalidArgument */
$title = htmlentities($title, ENT_QUOTES, $escapeTitle);
}
$templater = $this->templater();
$confirmMessage = null;
if (isset($options['confirm'])) {
$confirmMessage = $options['confirm'];
unset($options['confirm']);
}
if ($confirmMessage) {
$confirm = $this->_confirm('return true;', 'return false;');
$options['data-confirm-message'] = $confirmMessage;
$options['onclick'] = $templater->format('confirmJs', [
'confirmMessage' => h($confirmMessage),
'confirm' => $confirm,
]);
}
return $templater->format('link', [
'url' => $url,
'attrs' => $templater->formatAttributes($options),
'content' => $title,
]);
}
/**
* Creates an HTML link from route path string.
*
* ### Options
*
* - `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.
*
* @param string $title The content to be wrapped by `` tags.
* @param string $path Cake-relative route path.
* @param array $params An array specifying any additional parameters.
* Can be also any special parameters supported by `Router::url()`.
* @param array $options Array of options and HTML attributes.
* @return string An `` element.
* @see \Cake\Routing\Router::pathUrl()
* @link https://book.cakephp.org/3/en/views/helpers/html.html#creating-links
*/
public function linkFromPath(string $title, string $path, array $params = [], array $options = []): string
{
return $this->link($title, ['_path' => $path] + $params, $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
*
* - `block` Set to true to append output to view block "css" or provide
* custom block name.
* - `once` Whether 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.
* - `plugin` False value will prevent parsing path as a plugin
* - `rel` 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.
*
* @param 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.
* @param array $options Array of options and HTML arguments.
* @return string|null CSS `` or `` tag, depending on the type of link.
* @link https://book.cakephp.org/4/en/views/helpers/html.html#linking-to-css-files
*/
public function css($path, array $options = []): ?string
{
$options += ['once' => true, 'block' => null, 'rel' => 'stylesheet'];
if (is_array($path)) {
$out = '';
foreach ($path as $i) {
$out .= "\n\t" . (string)$this->css($i, $options);
}
if (empty($options['block'])) {
return $out . "\n";
}
return null;
}
if (strpos($path, '//') !== false) {
$url = $path;
} else {
$url = $this->Url->css($path, $options);
$options = array_diff_key($options, ['fullBase' => null, 'pathPrefix' => null]);
}
if ($options['once'] && isset($this->_includedAssets[__METHOD__][$path])) {
return null;
}
unset($options['once']);
$this->_includedAssets[__METHOD__][$path] = true;
$templater = $this->templater();
if ($options['rel'] === 'import') {
$out = $templater->format('style', [
'attrs' => $templater->formatAttributes($options, ['rel', 'block']),
'content' => '@import url(' . $url . ');',
]);
} else {
$out = $templater->format('css', [
'rel' => $options['rel'],
'url' => $url,
'attrs' => $templater->formatAttributes($options, ['rel', 'block']),
]);
}
if (empty($options['block'])) {
return $out;
}
if ($options['block'] === true) {
$options['block'] = __FUNCTION__;
}
$this->_View->append($options['block'], $out);
return null;
}
/**
* Returns one or many `