Give Me Cake

CakePHP Baking, Internet Marketing, SEO, Design and more

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()
)
No TweetBacks yet. (bookmark

Comments

One Response to “Image links with HtmlHelper”

  1. Martin
    November 5th, 2008 @ 11:02 pm

    When I don’t need all the features of the link method I prefer this way. A bit less code and just plain easier to read for me.

    $html->image(‘logo.png’,array(‘alt’=>’the logo’, ‘url’=>’/'));

Leave a Reply





  • Give me Cake?

    Givemecake.dev-work.com a collection of web application development tips, tutorials and resources.
  • See Your Ads Here

  • See Your Ads Here

  • Meta