cakephp - 将 html 属性分配给 CakePHP 的 HtmlHelper 图像中的 url 链接

标签 cakephp cakephp-2.0 html-helper

我试图找到一种方法来为 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->imageHtml->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...>将被转义,它会显示为 &lt;img ... &gt;

关于cakephp - 将 html 属性分配给 CakePHP 的 HtmlHelper 图像中的 url 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12271451/

相关文章:

php - Cakephp 3 - 从数据库中减去一个值

php - 取消订阅每月定期付款?

json - cakephp Web服务如何编写允许我上传文件的 Action

CakePHP 3案例计数查询返回true而不是整数

php - 如何清除 Controller CakePHP 2.x 中的缓存?

c# MVC Html helper + 表达式 + 去除丑陋

jquery - 在 HTML 帮助程序中使用 jQuery

ruby-on-rails - 在输出为 json 之前过滤模型的属性

cakephp - cakephp 3.8.13处理未经授权的请求,以JSON响应

mysql - 在 cakephp 中最后插入的 id