2014-05-04 40 views
2

在我的文章中,我有時會包含相同的HTML模式。 要做到這一點的話,我在_includes/創建pattern.html,包括它在我的職位有:檢測一個文件是否包含在Jekyll

{% include pattern.html %} 

我想補充我的header如果一個CSS文件(或任何其他代碼),當且僅當pattern.html在我的文章中已經使用過。把它放在僞代碼,我想獲得以下layouts/default.html

<!doctype html> 
<html> 
    <head> 
     {% if pattern.html is included in the post %} code here {% endif %} 
    </head> 
    <body> 
     {{ content }} 
    </body> 
</html> 

我已經嘗試過分配一個變量pattern.html和我的佈局進行測試,但出現這種assignement爲時已晚:佈局已經被處理。我知道我可以通過YALM傳遞一個變量,但我的目標是擺脫它。我寧願不使用插件來做到這一點。

回答

3

它似乎問這個問題幫助我回答它! 您可以使用液體contains爲了在您的文章中查找模式。

例如,如果所包括的pattern.html包含的代碼的特定件(<!--pattern-->例如,或任何特定的HTML),你可以在頭使用:

<!doctype html> 
<html> 
    <head> 
     {% if content contains "<!--pattern-->" %} code here {% endif %} 
    </head> 
    <body> 
    {{ content }} 
    </body> 
</html> 

這可以用於任何HTML圖案。例如,如果只想在頁面中存在代碼時調用產生合成着色的CSS文件,請使用:

<!doctype html> 
<html> 
    <head> 
     {% if content contains "<code>" %} 
      <link rel="stylesheet" href="code.css"> 
     {% endif %} 
    </head> 
    <body> 
    {{ content }} 
    </body> 
</html> 
相關問題