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"><i class="gicon-edit"></i></a>
How should the correct HTML code be written?
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.
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.
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.
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>.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With