CakePHP - 使用$ this-> Html->链接$ this-> Html-> image ....生成ascii而不是实际的HTML

时间:2013-02-21 16:30:04

标签: html cakephp

我正在使用cakephp 2.3.0。我在手册中搜索了很长时间,但我还没有找到答案。我正在尝试使用$ this-> Html->链接,以及$ this-> Html->图片。我正在尝试创建单击图像的功能。有关为什么生成引号的ascii渲染的任何想法?

以下是我的视图中的代码段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')),
            )
         )  
      );

以下是上述代码的实际HTML结果。如您所见,生成的HTML显示ascii版本的引号(“)和'<'和'>':

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

以下是我希望HTML的样子:

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

4 个答案:

答案 0 :(得分:21)

您需要将escape选项添加到link()来电的选项数组中。将其设置为false,如下所示:

echo $this->Html->link(
    $this->Html->image('mydog.jpg'), '/lol.html', array('escape' => false)
);

答案 1 :(得分:1)

是可以将图像制作为锚标记。  你只需要为它设置escape = false,如下所示: -

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

?>

答案 2 :(得分:1)

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

这是没有任何链接的普通图片,现在用链接标签包装

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

同样,您可以将图像标记分配给变量并使用它

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

答案 3 :(得分:0)

试试这个:

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

我已将图片链接到类deleteImg。