问题 CakePHP - 如何使用Helpers与target =“_ blank”建立图像链接


这似乎应该很简单,但我是CakePHP的新手。也许这只是我应该用良好的HTML编写的东西,但是 - 希望通过CakePHP的HTML帮助程序找到如何做到这一点。

我只想要一个目标=“_空白”的图像链接。

这是我试过的:

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

(所有在一条线 - 为了方便观看而分手了)

但是当我这样做时,我得到了这个:

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

任何帮助是极大的赞赏。


回答 (谢谢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
    )
); ?>

5378
2018-03-31 01:46


起源

答案非常有用,谢谢分享。保持! - Aditya P Bhatt


答案:


你需要告诉 HtmlHelper::link 不要HTML转义输入。
这一切都很好 记录在手册中


3
2018-03-31 04:02



谢谢!我现在读了大约7次 - 从未点击过。 - Dave


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

12
2017-10-10 12:10





您需要使用Html->图像。一探究竟:

http://book.cakephp.org/view/1441/image


0
2018-03-31 03:37



我已经使用过 - 事实上,我在上面的代码中使用它。如果我做错了,请举例说明 - 您提供的链接没有关于链接的目标属性的信息。 - Dave
它在手册中是正确的:<?php echo $ this-> Html-> image(“recipes / 6.jpg”,array(“alt”=>“Brownies”,'url'=> array('controller'=> 'recipes','action'=>'view',6))); ?>您看到使用url选项而不是image-> link中的调用html-> image - user470714
@ user470714 - 虽然我很感谢您尝试回答,但我建议您在下次回答之前阅读该问题。我没有问如何制作链接图像 - 我问如何在新的标签/窗口中使用target =“_ blank”打开链接图像。你的答案和评论都没有解决我的问题。 - Dave


就像在Cook Book中提到的那样,你可以使用image方法的'url'选项:

echo $this->Html->image("recipes/6.jpg", array(
   'alt' => "Brownies",
   'url' => array('controller' => 'recipes', 'action' => 'view', 6)
));

0
2018-06-19 18:35





确切的代码将是这样的

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

0
2017-07-03 11:12





echo $ html-> link(“more”,array('controller'=>'users','action'=>'index / welcome'),array('style'=>'_ blank'),false,false) ;?> image('more-arrow.png',array('alt'=>'more','height'=>'11','width'=>'17'))?>


-1
2018-04-05 13:04



我不认为这是对的。首先,它有两个php关闭标签,两个,它将“样式”设置为_blank而不是目标。感谢您花时间回答,但这个问题已经得到了解答。 :) - Dave
:]看着这段代码,我笑得很开心。从繁忙的一天回来的好。 - Fr0zenFyr