2015-05-29 45 views
1

我想顯示一個僅顯示不同類別的菜單。使用Jekyll顯示不同類別的菜單

想象有以下結構:

_folder1

  1. com1.html
  2. com2.html
  3. com3.html

現在,我只專注約三個文件。

對於包含在_folder1每個文件有以下YAML MATTER

TITLE:1文件
類型:Y
項目:1

標題:2File
類型:Y
項目:1

TITLE:3File
型:Y
項目:2

現在,我想表現出以下列表:

項目

和我不't want to have a double 1.

什麼是b在Jekyll中實現它的最佳實踐?

回答

1

這是可能的,但你需要一些醜陋的字符串操縱黑客來實現它。

據我所知,Liquid沒有正確的方法來自己創建數組。
因此,90%以下的解決方案包含濫用字符串以創建數組。

<!-- Step 1: create an array with all projects (with duplicates) --> 

{% for page in site.pages %} 
    {% if page.project %} 
     {% capture tmp %}{{ tmp }}#{{ page.project }}{% endcapture %} 
    {% endif %} 
{% endfor %} 

{% assign allprojects = tmp | remove_first: '#' | split: '#' | sort %} 


<!-- Step 2: create an array of unique projects (without duplicates) --> 

{% for project in allprojects %} 
    {% unless tmp2 contains project %} 
     {% capture tmp2 %}{{ tmp2 }}#{{ project | strip }}{% endcapture %} 
    {% endunless %} 
{% endfor %} 

{% assign uniqueprojects = tmp2 | remove_first: '#' | split: '#' | sort %} 


<!-- Step 3: display unique projects --> 

<h1>Projects:</h1> 
<ul> 
{% for project in uniqueprojects %} 
    <li>{{project}}</li> 
{% endfor %} 
</ul> 

最終,第3步會生成以下HTML ...準確的要求:

<h1>Projects:</h1> 
<ul> 
    <li>1</li> 
    <li>2</li> 
</ul>