9

I'm using cakephp 2.3.0. I searched in the manual for quite awhile, but I haven't found the answer. I'm trying to use $this->Html->link, along with $this->Html->image. I'm trying to create the ability to click on an image. Any ideas as to why the ascii rendering of quotes is being generated?

Here is my snippet codeset in my view ctp:

echo $this->html->tableCells(
        array(
            array(
                array (
                   $this->Html->link($myActivity['Activity']['name'], array('controller' => 'users', 'action' => 'edit'), array('title' => '')), 
                            array('align' => 'left')),
                    array ($myActivity['Activity']['status'], array('align' => 'left')),
                    array ($myActivity['Activity']['any_messages'], array('align' => 'left')),
                    $date2,
                    array ($this->Html->link(
                            $this->Html->image('pencil.jpg', array('alt' => 'Edit', 'border' => '0', 'width' => '25')), 
                            array('controller' => 'users', 'action' => 'add'), array('title' => ''))
                    ),
                    $this->Html->image('trashcan.jpg', array('alt' => 'Delete', 'border' => '0', 'width' => '25')),
                    $this->Html->image('copy.png', array('alt' => 'Copy', 'border' => '0', 'width' => '25')),
            )
         )  
      );

Below is the actual HTML result of the code above. As you can see, the generated HTML is showing ascii version of quotes (") and '<' and '>':

<tr>
    <td align="left">
        <a href="/activities/index.php/users/add" title="">Running</a>
    </td>
    <td align="left">Live</td>
    <td align="left">no</td>
    <td>02/18/13</td>
    <td>
        <a href="/activities/index.php/users/edit" title="">&lt;img src=&quot;/activities/app/webroot/img/pencil.jpg&quot; alt=&quot;Edit&quot; border=&quot;0&quot; width=&quot;25&quot; /&gt;</a>
    </td>
    <td>
        <img src="/activities/app/webroot/img/trashcan.jpg" alt="Delete" border="0" width="25">
    </td>
</tr>

Below is what I would expect the HTML to look like:

<tr>
    <td align="left">
        <a href="/activities/index.php/users/add" title="">Running</a>
    </td>
    <td align="left">Live</td>
    <td align="left">no</td>
    <td>02/18/13</td>
    <td>
        <a href="/activities/index.php/users/edit" title="">
            <img src="/activities/app/webroot/img/pencil.jpg" alt="Edit" border="0" width="25"></a>
    </td>
    <td>
        <img src="/activities/app/webroot/img/trashcan.jpg" alt="Delete" border="0" width="25">
    </td>
</tr>
1
  • complete duplicate of thousand others (see the sidebar)
    – mark
    Feb 21, 2013 at 17:31

4 Answers 4

22

You need to add the escape option to the options array of your link() calls. Set it to false, like this:

echo $this->Html->link(
    $this->Html->image('mydog.jpg'), '/lol.html', array('escape' => false)
);
5
  • Awesome, I knew this would be an easy fix. I'll give it a try later today when I have time. Thanks Matt.
    – Kevin
    Feb 21, 2013 at 16:44
  • Why do you have to add the escape option?
    – Jonathan
    May 7, 2014 at 20:54
  • @defaye If you don't set it to false then the contents of the link will be escaped, e.g. > will become &gt; and etc., which means HTML inside your link won't work.
    – Matt Cain
    May 8, 2014 at 1:39
  • @MattCain ah got it. Thanks. I noticed there's perhaps a better way of doing an image link by just having echo $this->Html->image("yourimage.jpg", array("alt" => "your alt", "url" => "http://yourlink")); am I missing why OP needs it the other way around?
    – Jonathan
    May 8, 2014 at 19:57
  • @defaye I didn't know that, that is a better way of doing it, unless you need to add attributes to the link like OP does, I don't think that provides a way of doing it.
    – Matt Cain
    May 9, 2014 at 11:17
2
echo $this->Html->image('imagename',array('alt'=>'myimage','class'=>'img-responsive'));

This is normal image without any link, now to wrap it with link tag use

echo $this->Html->link($this->Html->image('imagename',array('alt'=>'myimage', 'title'=>'myimage','class'=>'img-responsive')), [
                      'controller' => 'controllerName',
                      'action'     => 'actionName',
                      'id'         => $value['id'], //if any parameters are passed
                      ],['escape'    => false]);

Similarly you can assign the image tag to a variable and use it

$myImageVar = $this->Html->image('imagename',array('alt'=>'myimage','class'=>'img-responsive'));

echo $this->Html->link($myImageVar, [
                          'controller' => 'controllerName',
                          'action'     => 'actionName',
                          'id'         => $value['id'], //if any parameters are passed
                          ],['escape'    => false]);
1

Yes It's possible to make an image as anchor tag. You just needs to set escape = false for it like below :-

<?php
$thumb_img = $this->Html->image('yourimage.png',array('alt'=>'yoursite.com','class'=>'yourclass'));

echo $this->Html->link( $thumb_img, array('controller'=>'yourcontroller','action'=>'youraction'), array('escape'=>false));

?>
0
0

Try this :

echo $this->Html->link('', array(
   'controller' => 'Mycont',
   'action'     => 'deletepic',
   $id
), array(
   'confirm'    => 'Are you sure you want to delete the image?',
   'class'      => 'deleteImg'
));

I have linked image to in class deleteImg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.