2012-07-03 64 views
2

我在創建Google網站搜索自定義搜索引擎,並試圖在搜索結果中顯示某些元數據,使用條件標記來防止顯示某些內容。元數據是使用PageMaps存儲在我的網頁,這樣的:谷歌網站搜索:在搜索結果中定製包含元數據的豐富網頁摘要

<!-- 
    Google Search PageMap 
    <PageMap> 
     <DataObject type="document"> 
      <Attribute name="type">project</Attribute> 
      <Attribute name="title">Page title</Attribute> 
      <Attribute name="topic">Page topic</Attribute> 

      <Attribute name="topic_url">http://url.com/knowledge-hubs/resilient-catchments/river-restoration-partnerships/</Attribute> 
      <Attribute name="knowledge_hub">Page knowledge hub</Attribute> 
      <Attribute name="knowledge_hub_url">http://url.com/knowledge-hubs/resilient-catchments/</Attribute> 
      <Attribute name="sniffer_code">ABC123</Attribute> 
      <Attribute name="project_status">Active</Attribute> 
      <Attribute name="project_start">1 August 2010</Attribute> 
      <Attribute name="project_end">1 July 2012</Attribute> 
     </DataObject> 

     <DataObject type="thumbnail"> 
      <Attribute name="src">http://url.com/img.jpg</Attribute> 
      <Attribute name="width">100</Attribute> 
      <Attribute name="height">60</Attribute> 
     </DataObject> 
    </PageMap> 
--> 

有些生成所有這些信息的,其他人只是包括一些事情(例如,標題和類型)。

我希望能夠顯示此處包含的任何信息,具體取決於頁面。這依賴於webResult模板中的條件data-if屬性。

這是我到目前爲止有:

<article id="sniffer_webResult"> 
      <div class="gs-title"> 
       <a data-attr="{href:unescapedUrl,target:target}" class="gs-title"> 
        <!--<span data-if="typeof richSnippet != 'undefined'" data-body="richSnippet.document.title"></span>--> 
        <span data-body="html(title)"></span> 
       </a> 
      </div> 

      <div data-if="Vars.richSnippet" data-attr="0" data-body="render('thumbnail',richSnippet,{url:unescapedUrl,target:target})"></div> 
      <div class="gs-snippet" data-body="html(content)"></div> 
      <div data-if="typeof richSnippet != 'undefined'" class="meta"> 
       <!--<article data-if="richSnippet.document.type == 'project'">--> 
        <!--<div data-if="richSnippet.document.snifferCode != 'undefined'"><strong>Sniffer code: </strong><span data-body="richSnippet.document.snifferCode" data-attr="0"></span></div>--> 
        <div data-if="richSnippet && richSnippet.document.snifferCode"><strong>Sniffer code: </strong><span data-body="richSnippet.document.snifferCode" data-attr="0"></span></div> 
        <div data-if="richSnippet.document.projectStatus != null"><strong>Status: </strong><span data-body="richSnippet.document.projectStatus" data-attr="0"></span></div> 
        <div data-if="richSnippet.document.topic != null" class="topic"><strong>Topic: </strong><a data-attr="{href:richSnippet.document.topicUrl}" data-body="richSnippet.document.topic" data-attr="0"></a></div> 
        <div data-if="richSnippet.document.knowledgeHub != null" class="hub"><strong>Knowledge hub: </strong><a data-attr="{href:richSnippet.document.knowledgeHubUrl}" data-body="richSnippet.document.knowledgeHub" data-attr="0"></a></div> 
       <!--</article>--> 
      </div> 
     </article> 

(你可以看到我已經瞎評論的東西出來等)

我似乎無法獲得條件語句上班。有些屬性雖然工作得很好,所以我快到了。如網頁,其中不包括大多數屬性頁,給出的結果此錯誤:

TypeError: Cannot read property 'snifferCode' of undefined: richSnippet && richSnippet.document.snifferCode

我已經通過了谷歌的文檔here走了,但我還沒有發現它是特別全面。

任何想法非常感謝! :)

回答

2

決定在顯示它們之前,先下載XML路徑並解析服務器端的結果。這對頁面元數據的顯示方式和時間提供了更多的控制,總體來說它更強大。它不像它第一次出現那樣可怕!

richSnippets對於基本顯示非常有用,但我不覺得自己可以自信地控制它們的顯示,所以這是用於XML的另一個原因。

相關問題