2012-01-03 67 views
4

加元nofollow標記佈局我希望能夠從視圖中添加meta標籤(或控制器如果可能的話)在CakePHP中CakePHP的:從視圖

我有一個頁面像/mycontroller/myview但是當它與訪問像過濾器:

/mycontroller/myview/page:2/max_price:500

然後我要添加元沒有後續的標籤。

在HtmlHelper類中有一個meta方法。

當我這樣稱呼它:

$this->Html->meta('keywords', 'test test test', array('inline'=>false)); 

它創建了一個meta標籤是這樣的:

<meta name="keywords" content="test test test" /> 

然而,當我這樣稱呼它:

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

我自然會期待和想要它來創建此:

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

相反,我得到這個雖然:

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

我在做什麼錯?

回答

8

documentation page(最後一行)

如果你想添加自定義meta標籤,然後第一個參數應設置爲一個數組。爲了輸出機器人索引標記使用下面的代碼:

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

你的情況:

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

希望這有助於

+0

謝謝修復它。 :)順便說一句,當內聯爲假時,每個人總是仍然使用回聲,這實際上並不會迴應任何內容,並且在您沒有回聲的情況下調用它時仍然有效。當元素不是內聯時,我沒有使用回顯,是否有任何理由?我只問,因爲我看到很多。 – 2012-01-03 15:38:47

+0

= P你是對的,你不應該做一個回聲,如果元素不內聯..它不會改變任何東西,這只是一個壞習慣,我有.. srry – pleasedontbelong 2012-01-04 09:27:22

3

下面是this page代碼的一個微調的版本。我測試過它,和它的工作:

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

很明顯,你可以在一個單一的線 - 我只是把它弄壞了下來,以便於查看這裏寫這篇文章。

+0

+1謝謝,你和gladontbelong是都是正確的,但我只能接受一個。 – 2012-01-03 15:37:00

+0

你應該回應這一個 – 2014-08-01 09:55:21

1

您可以從以期從控制器設置相同的方式佈局設置變量使用$this->set()查看,我想有這樣的設置:

// View 
if($condition) { 
    $this->set('nofollow', true); 
} 

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

} 

現在你有一個很短的1襯墊從任何視圖文件添加nofollow指令。

+0

+1也很好的信息,謝謝。 :) – 2012-01-03 15:35:31