Image links with HtmlHelper
Posted on | October 8, 2008 | 1 Comment
To create an image link with Cakephp’s HtmlHelper, you need to pass in the $html->image into $html->link.
First step to use the HtmlHelper in Cakephp, you need to include the helper in the controller.
Using:
var $helpers = array('Html', 'Form');
In your views, (.ctp files), you will call with
<?php echo $html->link($html->image('rss_text.jpg',
array('class'=>'img-icon', 'title' => 'Subscribe to RSS')
),
array('controller' => 'features', 'action' => 'feed.rss'),
array('escape' => false, 'title' => 'Subscribe to RSS')
);
?>
Note that the second parameter for $html->link is an array instead of a string. This is to allow $html->url to handle the url and manipulate it. I recommend doing it this way to have more flexible and less headache when you need to change the pathing for your application.
You need to have escape == false in for the html attributes for $html->link to prevent the html returned from html->image being encoded. You can set the fifth parameter of $html->link to false instead. It will work too. Refer below for the function declaration.
References : Cakephp API
HtmlHelper::link ( $ title, $ url = null, $ htmlAttributes = array(), $ confirmMessage = false, $ escapeTitle = true ) HtmlHelper::image ( $ path, $ options = array() )




















