TL; DR: Microdata強制您使用itemprop作爲此itemprops值的itemtype。如果你想把你的模板分解成部分,你如何處理這個問題?該元素可能只出現在兩個地方之一中,無論是在包含模板中還是在包含的部分中,這兩者都不能很好地工作。Microdata:如何使用模板/部分?
我正在使用haml的Rails,但我想我的問題適用於任何模板引擎,您可以將模板分割成更小的部分。我大量使用partials,這意味着我的模板通常不會超過10行代碼。但將微數據擬合到這些看起來過於複雜,那麼你應該如何恰當地使用微數據和偏微分?
拿這個例子:
#app/views/article/show.html.haml
%p Hey, Welcome! Read This wonderful article:
= render @article
#app/views/articles/_article.html.haml
%div[ article ]
%h1= @article.title
%p= @article.description
%div= render @article.video
#app/views/videos/_video.html.haml
%div[ video ]
%h2= video.file_name
%video{ src: video.url }
現在用微:
#app/views/articles/_article.html.haml
%div[ article ]{ itemscope: true, itemtype: 'http://schema.org/Article' }
%h1{ itemprop: 'name' }= @article.title
%p{ itemprop: 'description' }= @article.description
%div{ itemprop: 'video' }= render @article.video
然後呢?我必須在同一個div上添加VideoObject
itemtype,它具有itemprop: 'video'
,因爲這就是微數據的工作原理。所以:
#app/views/articles/_article.html.haml
%div{ itemprop: 'video', itemscope: true, itemtype: 'http://schema.org/VideoObject' }= render @article.video
但是,然後我不能在視頻部分再次使用相同的itemtype。這現在將無效:
#app/views/videos/_video.html.haml
%div[ video, itemscope: true, itemtype: 'http://schema.org/VideoObject' ]
但是itemtype屬於視頻。我不想在每一個容器元素中重複它。我希望能夠在其他地方使用部分視頻,並且仍然會將其視爲具有微數據的視頻。所以,整個事情已經搬出物品的部分,進入VIDO部分是這樣的:
#app/views/articles/_article.html.haml
%div= render @article.video
#app/views/videos/_video.html.haml
%div[ video ]{ itemprop: 'video', itemscope: true, itemtype: 'http://schema.org/VideoObject' }
同樣,itemprop必須爲視頻項目類型相同的元素。但爲什麼視頻部分關心/知道itemprop是如何被調用的,這是它的價值?
我唯一的解決方案是將itemprop傳遞給partial。這也意味着,如果沒有傳遞,我們必須聲明一個默認的itemprop。所以我們結束了這個:
#app/views/articles/_article.html.haml
%div= render @article.video, itemprop: 'video'
#app/views/videos/_video.html.haml
itemprop ||= nil
%div[ video ]{ itemprop: itemprop, itemscope: true, itemtype: 'http://schema.org/VideoObject' }
這似乎過於複雜。另外我發現將itemprop的名稱傳遞到partial中會讓人感到困惑,因此partial會知道itemprop被調用的是它的值。這有多奇怪?在編程的其他地方,我遇到了需要知道指向它們的事物名稱的對象。
那麼有沒有更好的方法來做到這一點?或者,這是怎麼回事,我不應該這麼嘮叨呢? ☺
支持這樣一個雄辯的問題! – 2014-09-05 09:32:07
@RichPeck是的,在完成後我想,這應該是一個博客文章,而不是。但是再一次,我沒有博客,所以,呃。 ;) – Conkerchen 2014-09-05 09:36:49
唯一的問題是像我這樣的人想看到一個簡短的問題來回答:D需要大量的腦力來制定答案,而不必解決原始問題。你可以加一個旁邊給我們一個更簡潔的問題嗎? – 2014-09-05 09:39:55