如何从CakePHP中的HTMLHelper向使用image方法创建的链接添加属性?

如何从CakePHP中的HTMLHelper向使用image方法创建的链接添加属性?

问题描述:

I'm using CakePHP.

This line of code will generate a <img> tag:

$html->image('image.png', array('alt', 'Alternative text'));

And this will generate the same thing, but it will make the image clickable:

$html->image('image.png', array('alt' => 'Alternative text', 'url' => 'http://www.example.com'));

So far I understand, but what can I do if I want to add attributes to the <a> tag.

Doing this:

$html->image('image.png', array('alt' => 'Alternative text', 'url' => 'http://www.example.com/', 'class' => 'aClass'));

Will add the attribute to <img> instead of <a>. The output is something like this:

<a href="http://www.example.com/">
    <img src="image.png" alt="Alternative text" class="aClass" />
</a>

But I want something like this:

<a href="http://www.example.com/" class="aClasse">
    <img src="image.png" alt="Alternative text" />
</a>

I tried to do like a $html->link() using a $html->image() as the first parameter but it didn't work.

Any idea?

Yes, you'll need to use the HTML helper link method and tell it to not escape the title, something it does by default, by adding the 'escape' => false parameter. Please read the manual, it's explained there: http://book.cakephp.org/view/1442/link