How to write HTML tag in CakePHP "link"

ID : 274344

viewed : 37

Tags : cakephpcakephp





Top 4 Answer for How to write HTML tag in CakePHP "link"

vote vote

95

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.

vote vote

87

You might find it easier to handle this sort of link using the url method of the HTML helper:-

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

This still properly routes the URL, but can make writing the anchor tag exactly as you want a lot simpler.

I personally take this approach when I don't want just simple text in a link as it can be more readable than using the link method with 'escape'=>false.

vote vote

72

Expanding on drmonkeyninja's answer:

For CakePHP 3.X you would use:

<a data-original-title=" Edit " data-placement="left" rel="tooltip" href="<?php echo $this->Url->build(array('controller'=>'static_pages','action'=>'edit',$page['StaticPage']['id'])) ?>" class="btn btn-small">   <i class="gicon-edit"></i> </a> 

($this->Url->build instead of $this->Html->url)

vote vote

63

Top 3 video Explaining How to write HTML tag in CakePHP "link"







Related QUESTION?