Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - how to use Helpers to make an image link with target="_blank"

This seems like it should be simple, but I'm new to CakePHP. Maybe it's just something I should write in good ole HTML, but - was hoping to find out how do to this with CakePHP's HTML helper.

I just want an image link that has target="_blank".

This is what I tried:

<?php echo $this->Html->link($this->Html->image('tmp/728x90.jpg',
    array('alt'=>'advertisement', 'height'=>'90', 
    'width'=>'728')),'http://www.google.com', array('target'=>'_blank')); ?>

(all in one line - just broke up for ease of viewing)

But when I do this, I get this:

<a href="http://www.google.com" target="_blank">&lt;img src=&quot;/img/tmp/728x90.jpg&quot; alt=&quot;advertisement&quot; height=&quot;90&quot; width=&quot;728&quot; /&gt;</a>

Any help is greatly appreciated.


Answer (thanks deceze)

<?php 

$image = $this->Html->image(
    'tmp/300x600.jpg', 
    array(
        'alt'=>'advertisement', 
        'height'=>'600', 
        'width'=>'300'
    )
);

echo $this->Html->link(
    $image,
    'http://www.google.com', 
    array(
        'target'=>'_blank', 
        'escape' => false
    )
); ?>
like image 333
Dave Avatar asked Mar 31 '11 01:03

Dave


3 Answers

<?php echo $this->Html->link($this->Html->image('fb2.jpg',array('alt'=>'facebook', 'height'=>'90','width'=>'728')),'http://www.facebook.com', array('target'=>'_blank','escape'=>false)); ?>
like image 188
sourav Avatar answered Nov 05 '22 11:11

sourav


You need to tell HtmlHelper::link not to HTML escape the input.
This is all very well documented in the manual.

like image 3
deceze Avatar answered Nov 05 '22 12:11

deceze


The exact code will be like this

  <?php 
         echo $this->Html->link(
                    $this->Html->image('tmp/728x90.jpg',
                                         array(
                                        'alt'=>'advertisement', 'height'=>'90',
                                        'width'=>'728')
                                       ),
                                    'http://www.google.com',
                                    array(
                                       'target'=>'_blank',
                                       'escape'=>false)
                                ); 
?>
like image 1
Shrish Shrivastava Avatar answered Nov 05 '22 13:11

Shrish Shrivastava