How to write HTML tag in CakePHP "link"

Mohit Tripathi picture Mohit Tripathi · Jun 6, 2013 · Viewed 37.4k times · Source

I am using the CakePHP 2.2 and need to write following code -

<a data-original-title=" Edit " data-placement="left" rel="tooltip" href="/admin/static_pages/edit/1" class="btn btn-small">
  <i class="gicon-edit"></i>
</a>

I have written the following code in CakePHP -

 <?php echo $this->Html->link($this->Html->tag('i', '', array('class' => 'gicon-edit')),array('controller'=>'static_pages','action'=>'edit',$page['StaticPage']['id']), array('rel'=>'tooltip','data-placement'=>'left','data-original-title'=>'Edit','class'=>'btn btn-small'));  ?>

and getting the following result -

<a class="btn btn-small" data-original-title="Edit" data-placement="left" rel="tooltip" href="/erudites/admin/static_pages/edit/1">&lt;i class="gicon-edit"&gt;&lt;/i&gt;</a>

How should the correct HTML code be written?

Answer

Dave picture Dave · Jun 6, 2013

Explanation:

Adding the 'escape'=>false option to your link makes it so it doesn't try to translate ('escape') all your html characters.

Also, I rarely (if EVER) find it helpful to use CakePHP's ->tag(). Just write the tag - much easier (and more efficient).

Example code:

echo $this->Html->link(
   '<i class="gicon-edit"></i>',
    array(
        'controller'=>'static_pages',
        'action'=>'edit',
        $page['StaticPage']['id']
    ),
    array(
        'rel'                 => 'tooltip',
        'data-placement'      => 'left',
        'data-original-title' => 'Edit',
        'class'               => 'btn btn-small',
        'escape'              => false  //NOTICE THIS LINE ***************
    )
);

Details here: http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

PS Obviously the code could be a 1-liner if you'd rather - just broke it up here for ease of reading.