Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write HTML tag in CakePHP "link"

Tags:

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?

like image 657
Mohit Tripathi Avatar asked Jun 06 '13 14:06

Mohit Tripathi


People also ask

How to create a hyperlink in HTML?

The HTML <a> tag defines a hyperlink. It has the following syntax: The most important attribute of the <a> element is the href attribute, which indicates the link's destination. The link text is the part that will be visible to the reader. Clicking on the link text, will send the reader to the specified URL address.

What does the <link> tag do in HTML?

When used for style sheets, the <link> tag is supported in all major browsers. No real support for anything else. Required. Specifies the relationship between the current document and the linked document Specifies the size of the linked resource. Only for rel="icon" The <link> tag also supports the Global Attributes in HTML.

What is the syntax of <a> tag in HTML?

It has the following syntax: The most important attribute of the <a> element is the href attribute, which indicates the link's destination. The link text is the part that will be visible to the reader. Clicking on the link text, will send the reader to the specified URL address.

How do I use an image as a link in HTML?

HTML Links - Use an Image as a Link. To use an image as a link, just put the <img> tag inside the <a> tag: Example. <a href="default.asp">. <img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;">. </a>.


2 Answers

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.

like image 136
Dave Avatar answered Sep 29 '22 10:09

Dave


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.

like image 30
drmonkeyninja Avatar answered Sep 29 '22 11:09

drmonkeyninja