我正试图找到一种方法,为CakePHP的HtmlHelper的image方法生成的链接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
方法将HTML属性添加到Anchor标记中,方法是将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 ... >
一样显示
如果您想使用HtmlHelper
向HTML anchor tag
添加任何属性,则可以通过:使用
<?php echo $this->Html->link(
$this->Html->image("loading.gif", array('alt' => 'Brownies', 'border' => '0')),
array('controller' => 'recipes', 'action' => 'view', 6), array('class' => 'picture', 'escape' => false));
如果您仍在寻找答案,您需要使用$this->Html->image
,而不是使用带有可选URL属性的$this->Html->image
,而是使用带有可选escape = false
属性的$this->Html->link
,其中链接的标题是您的图像。escape = false
所做的是在链接的标题中取消隐藏特殊字符,从而允许您使用图像或其他html元素。
以下是的示例
echo $this->Html->link(
$this->Html->image($image['Image']['file'], 'class' => 'image', 'alt' => $image['Image']['title'])),
'path/to/image', // or an array('controller' => 'mycontroller', 'action' => 'myaction')
array('escape' => false));
您可以添加更多的图像属性,如我列出的class和alt,以及更多的链接属性,如我们列出的escape。