CakePHP:从视图向布局添加meta-nofollow标记

CakePHP:从视图向布局添加meta-nofollow标记,cakephp,cakephp-1.3,Cakephp,Cakephp 1.3,我希望能够在CakePHP中从视图(或控制器,如果可能)添加元标记 我有一个类似于/mycontroller/myview的页面,但当使用以下过滤器访问它时: /mycontroller/myview/page:2/最高价格:500 然后我想添加meta-no-follow标签 HtmlHelper类上有一个方法 当我这样称呼它时: $this->Html->meta('keywords', 'test test test', array('inline'=>false));

我希望能够在CakePHP中从视图(或控制器,如果可能)添加元标记

我有一个类似于
/mycontroller/myview
的页面,但当使用以下过滤器访问它时:

/mycontroller/myview/page:2/最高价格:500

然后我想添加meta-no-follow标签

HtmlHelper类上有一个方法

当我这样称呼它时:

$this->Html->meta('keywords', 'test test test', array('inline'=>false));
<meta name="keywords" content="test test test" />
$this->Html->meta('robots', 'noindex, nofollow', array('inline'=>false));
// View
if($condition) {
    $this->set('nofollow', true);
}

// Layout (in <head>)
if(isset($nofollow) && $nofollow) {
    echo $this->Html->meta(array('name' => 'robots', 'content' => 'noindex, nofollow'));

}
它创建一个元标记,如下所示:

$this->Html->meta('keywords', 'test test test', array('inline'=>false));
<meta name="keywords" content="test test test" />
$this->Html->meta('robots', 'noindex, nofollow', array('inline'=>false));
// View
if($condition) {
    $this->set('nofollow', true);
}

// Layout (in <head>)
if(isset($nofollow) && $nofollow) {
    echo $this->Html->meta(array('name' => 'robots', 'content' => 'noindex, nofollow'));

}
我自然希望并且希望it能够创建以下内容:

<meta name="robots" content="noindex, nofollow" />

然而,我得到的却是:

<link href="http://www.example.com/mycontroller/noindex, nofollow" type="application/rss+xml" rel="alternate" title="robots" />

我做错了什么?

(最后一行)

如果要添加自定义元标记,则应将第一个参数设置为数组。要输出robots noindex标记,请使用以下代码:

就你而言:

echo $this->Html->meta(array('name' => 'robots', 'content' => 'noindex, nofollow'),null,array('inline'=>false));

希望这对您有所帮助

这里是一个经过调整的版本。我已经测试过了,它确实有效:

<?php
echo $this->Html->meta(
    array('name' => 'robots', 'content' => 'noindex, nofollow'),
    null,
    array('inline'=>false));
?>


显然,您可以在一行中写入此内容-为了便于查看,我在这里对其进行了分解。

您可以使用与使用
$this->set()
从控制器到视图相同的方式,将变量从视图设置到布局,我将有如下设置:

$this->Html->meta('keywords', 'test test test', array('inline'=>false));
<meta name="keywords" content="test test test" />
$this->Html->meta('robots', 'noindex, nofollow', array('inline'=>false));
// View
if($condition) {
    $this->set('nofollow', true);
}

// Layout (in <head>)
if(isset($nofollow) && $nofollow) {
    echo $this->Html->meta(array('name' => 'robots', 'content' => 'noindex, nofollow'));

}
//查看
如果($条件){
$this->set('nofollow',true);
}
//布局(英寸)
if(isset($nofollow)&&$nofollow){
echo$this->Html->meta(数组('name'=>'robots','content'=>'noindex,nofollow');
}

现在,您有了一个简短的1行程序,可以从任何视图文件中添加nofollow指令。

+1谢谢,您的和PleaseDontAttribute都是正确的,但我只能接受一个。您应该回显这一行,感谢您修复了它。:)顺便说一句,我看到当inline为false时,每个人都会使用echo,这实际上并不回显任何内容,当您只调用它而不使用echo时,它仍然有效。当元素没有内联时,我没有使用echo,有什么理由这样做吗?我之所以问这个问题,是因为我看到很多人都这样做。。这不会改变什么,只是我的一个坏习惯。。对不起