CakePHP Span tag in Anchor tag

I am trying to output a CakePHP link that looks like this:

 <a href="/foo/bar" class="some other classes">
     <span class="icon new"></span>FooBar</a>

So I use the following code in my view

    <?php 
echo $this->Html->link(
$this->Html->tag('span', null, array('class' => 'icon new')) . "FooBar",
        array('controller' => 'foo', 'action' => 'bar'),
        array('class' => 'some other  classes', 'escape' => false)
     );
?> 

However, CakePHP outputs the following:

<a href="/foo/bar" class="some other classes">
     <span class="icon new">FooBar</span></a>

What violates my design. How can I get CakePHP to add <span>"FooBar" tags after ?

EDIT: Its also worth mentioning that I know that tags <span>should not be in the anchor tag normally, but in the case where it should.

+5
source share
4 answers

You need to use an empty string instead nullas text for the range, then your code will work as expected.

HtmlHelper, null "" , . :

https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper/HtmlHelper.php#L906

, ;

echo $this->Html->link(
    $this->Html->tag('span', '', array('class' => 'icon new')) . "FooBar",
    array('controller' => 'foo', 'action' => 'bar'),
    array('class' => 'some other  classes', 'escape' => false)
);

</span>

, , :

</span> , CakePHP, "" . HTML , , , HTML:

<a href="/foo/bar" class="some other classes">
 <span class="icon new">FooBar</a>

, 'span'

<span> , , "" . </span> ( </a>).

"" HTML, . , (, , JavaScript).

PHP-, ,

+5

CakePHP, IDE.

- :

<a href="<?php echo Router::url(array('controller'=>'myController', 'action'=>'index'))?>"><span class="icon-new"></span>Foobar</a>
+1

It looks a bit overwhelming to me. Just do the following:

echo $this->Html->link(
   "<span class="icon new"></span> FooBar",
    array('controller' => 'foo', 'action' => 'bar'),
    array('class' => 'some other  classes', 'escape' => false)
);

I have been using CakePHP for 4 years and I do not see the benefits of using a tag in this instance.

0
source

Can you use regular PHP in this case?

I think you could do it like this:

<?PHP
   echo('<a href="' . '/foo/bar' . '" class="' . 'some other classes' . '"><span class="' . 'icon new' . '"></span>' . 'FooBar' . '</a>')
?>
-3
source

All Articles