20

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?

3 Answers 3

38

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Dave, I have applied the code u suggested, it runs but giving me an extra "<i>" tag like the following - <a class="btn btn-small" data-original-title="Edit" data-placement="left" rel="tooltip" href="/erudites/admin/static_pages/edit/1"> <i class="gicon-edit"></i> </a> <i class="gicon-edit"> <a class="btn btn-small" data-original-title="View" data-placement="top" rel="tooltip"> <a class="btn btn-inverse btn-small" data-original-title="Remove" data-placement="bottom" rel="tooltip"> </i>
@Dave, I have done it just by applying "</i>", thanks for your help :)
2

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.

2 Comments

The only down-side is lack of customization later. As a random example, you could extend the helper to give all links that went to the "posts" controller a specific class to change their color...etc etc. By using the helper, you leave all that fun stuff open/available
True. Though that's not something I've ever needed to achieve personally. I prefer that sort of behaviour actually in my templates.
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.