Mohit Tripathi
Mohit Tripathi

Reputation: 227

How to write HTML tag in CakePHP "link"

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?

Upvotes: 20

Views: 38769

Answers (3)

Dave
Dave

Reputation: 29121

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.

Upvotes: 38

Chaim
Chaim

Reputation: 2149

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)

Upvotes: 2

drmonkeyninja
drmonkeyninja

Reputation: 8540

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.

Upvotes: 2

Related Questions