CakePHP - using $this->Html->link with $this->Html->image…generating ascii instead of actual HTML

荒凉一梦 提交于 2020-02-18 13:59:38

问题


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:


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



回答2:


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

?>



回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/15007396/cakephp-using-this-html-link-with-this-html-image-generating-ascii-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!