我试图找到一种方法来为 CakePHP 的 HtmlHelper 的图像方法生成的链接 URL 设置属性。我正在使用 CakePHP 2.2.1
例如下面的代码:
echo $this->Html->image("recipes/6.jpg", array(
"alt" => "Brownies",
'url' => array('controller' => 'recipes', 'action' => 'view', 6)
));
生成:
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
如何向 href 标签添加属性。例如,class='picture' 看起来像:
<a href="/recipes/view/6" class='picture'>
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
最佳答案
您不能通过 Html->image
向 anchor 标记添加 HTML 属性方法 - 方法是将 Html->image
在 Html->link
内像这样的方法:
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array('alt' => 'Brownies')),
array('controller' => 'recipes', 'action' => 'view', 6, array('escape'=>false', 'class'=>'picture')
);
您还必须包括 'escpape'=>false
- 否则你的 <img...>
将被转义,它会显示为 <img ... >
关于cakephp - 将 html 属性分配给 CakePHP 的 HtmlHelper 图像中的 url 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12271451/