CakePHP跨度标记在锚标记内 [英] CakePHP Span Tag within Anchor Tag

查看:172
本文介绍了CakePHP跨度标记在锚标记内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让CakePHP输出一个看起来像这样的链接:

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

所以我在我的视图中使用下面的代码

 <?php 
echo $ this-> Html-> link(
$ this-> Html-> tag ,null,array('class'=>'icon new'))FooBar,
array('controller'=>'foo','action'=>'bar' b $ b array('class'=>'some others classes','escape'=> false)
);
?>然而,CakePHP输出以下内容:



<$ p



$ b $ p> < a href =/ foo / barclass =some other classes>
< span class =icon new> FooBar< / span>< / a>

这会打破我的设计。如何让CakePHP在< span> gt; 标签之后附加FooBar



编辑:还值得一提的是,我知道< span> 标签通常不应该在锚标签中, / p>

解决方案

您需要使用空字符串替换 null



查看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' '=> false)
);



关闭< / span>



对于那些想知道的人,有一点解释:



> < / span> 在您的示例中实际上是不存在在CakePHP生成的输出,但是由您的浏览器自动添加。如果您在浏览器中查看HTML的,您会看到这是HTML中的实际内容:

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

如您所见,无法关闭'span'

由于< span> 未关闭,浏览器将尝试更正此错误并自动假设您关闭它。因此,它将在它找到的下一个标签之前添加一个结束< / span> (在这种情况下结束< / a> )。



浏览器中的检查器将始终显示浏览器使用呈现输出的HTML。这包括由浏览器动态生成的元素(例如通过JavaScript添加的元素)进行的自动更正。



要检查输出您的PHP脚本,始终查看,而不是检查器


I'm trying to have CakePHP output a 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>

Which breaks my design. How can I get CakePHP to append "FooBar" after the <span> tags?

EDIT: Its also worth mentioning that I know a <span> tags shouldn't be within an anchor tag usually, but in the case its a must.

解决方案

You need to use an empty string in stead of null as the text for the span, then your code will work as expected.

Looking at the source code of the HtmlHelper, null is treated as a 'special' value, causing only the opening tag of the span to be created. You can see this in this line:

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

Change your code to this and it should work;

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)
);

Additional explanation of the closing </span>

A bit of explanation, for those who wonder:

The closing </span> in your example is actually not present in the output generated by CakePHP, but automatically 'added' by your browser. If you view the source of the HTML in your browser, you'll see that this is what is actually in your HTML:

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

As you can see, no closing 'span'

Because the <span> is not closed, the browser will try to correct this error and automatically assumes that you 'forgot' to close it. Therefor it will add a closing </span> before the next tag it finds (in this case the closing </a>).

The 'inspector' in your browser will always show the HTML that the browser uses to render the output. This includes automatic corrections made by the browser and dynamically generated elements (e.g. Elements added via JavaScript).

To check the output of your PHP scripts, always view the source, not the inspector

这篇关于CakePHP跨度标记在锚标记内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆