Did you know? image() can has url!
Now this is a quick one here. Most of you know the following code from the default.ctp after you “just baked” the app.
echo $html->link(
$html->image('cake.power.gif', array('alt'=> __("CakePHP...", true), 'border'=>"0")),
'http://www.cakephp.org/',
array('target'=>'_blank'), null, false
);
This displays the small “CakePHP Power” badge with a link to their website. Much code for just a image surrounded with a link, eh?
The $html->image() method has a option called ‘url’ that automatically does this for you. Only limitation is that you can’t pass attributes to the link tag. But who really needs “target=_blank” in XHTML world anyway?
So here’s the same thing, only without the clutter:
echo $html->image('cake.power.gif', array('url' => 'http://www.cakephp.org', 'alt' => __('CakePHP...', true), 'border' => 0));
Tada! There you have it. Of course ‘url’ accepts arrays and everything else since it passes the value straight into Helper::url();
Enjoy


This is also in the cookbook: http://book.cakephp.org/view/206/Inserting-Well-Formatted-elements ;-).
The only downside with using the ‘url’ option of HtmlHelper::image is that you cannot place any attributes (e.g. id, class, etc.) on the generated anchor element.
@Joel Perras:
“The only downside with using the ‘url’ option of HtmlHelper::image is that you cannot place any attributes”
reads alot like
“Only limitation is that you can’t pass attributes to the link tag.”
.. but i can’t remember where i’ve read the latter :-)
Regarding the book: Yeah, but that info about image() is hidden in the link() section. Hmm.. needs fix. The whole page is a mess imho. Maybe i’ll do something about it.
Thanks for you comment