php CakePHP - 如何使用 Helpers 制作带有 target="_blank" 的图像链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5494470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP - how to use Helpers to make an image link with target="_blank"
提问by Dave
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.
这看起来应该很简单,但我是 CakePHP 的新手。也许这只是我应该用好的 ole HTML 编写的东西,但是 - 希望找到如何使用 CakePHP 的 HTML 帮助程序来做到这一点。
I just want an image link that has target="_blank".
我只想要一个具有 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"><img src="/img/tmp/728x90.jpg" alt="advertisement" height="90" width="728" /></a>
Any help is greatly appreciated.
任何帮助是极大的赞赏。
Answer(thanks deceze)
回答(感谢 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
)
); ?>
采纳答案by deceze
You need to tell HtmlHelper::link
not to HTML escape the input.
This is all very well documented in the manual.
回答by sourav
<?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)); ?>
回答by Shrish Shrivastava
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)
);
?>
回答by Phil123
Like mentioned in the Cook Book, you can use the 'url' option of the image method:
就像在 Cook Book 中提到的那样,您可以使用 image 方法的 'url' 选项:
echo $this->Html->image("recipes/6.jpg", array(
'alt' => "Brownies",
'url' => array('controller' => 'recipes', 'action' => 'view', 6)
));
回答by user470714
You need to use the Html->image . Check it out:
您需要使用 Html->image 。一探究竟:
回答by Archit Patel
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'))?>
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'))?>