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

22,868

Solution 1

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

Solution 2

<?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)); ?>

Solution 3

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)
                                ); 
?>
Share:
22,868
Dave
Author by

Dave

Artist / Designer / Programmer

Updated on July 09, 2022

Comments

  • Dave
    Dave almost 2 years

    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
        )
    ); ?>
    
  • Dave
    Dave about 13 years
    I have used that - and in fact, I'm using it in my code above. Please give an example if I'm doing something wrong - the link you gave has no information regarding the target attribute for a link.
  • user470714
    user470714 about 13 years
    It's right in the manual: <?php echo $this->Html->image("recipes/6.jpg", array( "alt" => "Brownies", 'url' => array('controller' => 'recipes', 'action' => 'view', 6) )); ?> You see you use the url option instead of the calling html->image inside of image->link
  • Dave
    Dave about 13 years
    @user470714 - While I do appreciate your attempt at answering, I recommend reading the question before answering next time. I did not ask how to make a linked image - I asked how to make a linked image open in a new tab/window with target="_blank". Neither your answer, nor your comment addressed my question.
  • Dave
    Dave about 13 years
    I don't think that is right. For one, it's got two php close tags, and two, it's setting "style" to _blank instead of target. I appreciate your taking the time to answer but this question has already been answered. :)
  • Fr0zenFyr
    Fr0zenFyr about 11 years
    :] I'm having a good laugh looking at this code. Nice refresher from the hectic day.