Why we use escape => false in cakephp

3.6k views Asked by At
echo $this->Html->link(
    '<span class="glyphicon glyphicon-remove"></span> Cancel',    
    array(
        'action'=>'index', 
        'page:'.$this->request->data['Transaction']['page']
    ),
    array(
        'class'=>'btn btn-default', 
        'escape'=>false
    ), 
    'Do you want to cancel ?'
);
3

There are 3 answers

0
Álvaro González On BEST ANSWER

Because your label is HTML rather than plain text.

You don't want to insert a literal <span class="glyphicon glyphicon-remove"></span> text, you want an HTML tag to display an icon.

    // lib/Cake/View/Helper/HtmlHelper.php
    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)) {
        $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
    }
0
d.coder On

HTML special characters in $title will be converted to HTML entities. To disable this conversion, we set the escape option to false in the $options array:

$this->Html->link($title, $url, $options);

Read: Manual

0
SrQ On

All HTML characters will be escaped on Views by default, just as if you used htmlentities() on it, so for this reason, all elements created by CakePHP Helpers ($this->Html->link() in this case) need the param 'escape' => false to avoid that conversion, be it trying to use nested tags or nested HTML in the label or so.

Reference