Cake\View\Helper\HtmlHelper::scriptBlock PHP Метод

scriptBlock() публичный Метод

### Options - safe (boolean) Whether or not the $script should be wrapped in - block Set to true to append output to view block "script" or provide custom block name.
public scriptBlock ( string $script, array $options = [] ) : string | null
$script string The script to wrap
$options array The options to use. Options not listed above will be treated as HTML attributes.
Результат string | null String or null depending on the value of `$options['block']`
    public function scriptBlock($script, array $options = [])
    {
        $options += ['safe' => true, 'block' => null];
        if ($options['safe']) {
            $script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
        }
        unset($options['safe']);
        $out = $this->formatTemplate('javascriptblock', ['attrs' => $this->templater()->formatAttributes($options, ['block']), 'content' => $script]);
        if (empty($options['block'])) {
            return $out;
        }
        if ($options['block'] === true) {
            $options['block'] = 'script';
        }
        $this->_View->append($options['block'], $out);
    }

Usage Example

Пример #1
0
 /**
  * Returns a Javascript code block
  * @param string $code Javascript code
  * @param array $options Array of options and HTML attributes
  * @return mixed A script tag or `null`
  */
 public function scriptBlock($code, array $options = [])
 {
     $options = $this->optionsDefaults(['block' => true], $options);
     return parent::scriptBlock($code, $options);
 }