2016-08-25 7 views
0

jekyllで生成されたサイトにcsvファイルの情報を表示したいと考えています。私はcsvファイルで適切なカテゴリを検索し、その中の4つをページに表示する必要があります。選択したカテゴリへのフィルタリングは問題ありませんが、出力を4つに制限するのは難しいです。液体中のデータを表示

if文に制限を適用する方法はありますか?あるいはこれを書くための他の方法がありますか?私は液体に精通していないので、明らかな解決策が見当たりません。

適用可能なすべてのデータを画面上に表示させるための基本コード:

 {% for study in site.data.studies %} 
     {% if study.category contains "foo" %} 
      <div class="col-sm-3"> 

       <h3>{{ study.title }}</h3> 
       <div class="list-of-attributes"> 

       <h6>Attributes: </h6> 
       {{ study.attributes }} 
       </div> 
      </div> 
     {% else %}  
      {% continue %} 
     {% endif %} 

     {% endfor %} 

私も全く働いていたどちらもunlesstablerowを、試してみました。私は少なくとも正しい道を歩いていますか?このforloopを4つのアイテムで止めるにはどうすればよいですか?

ありがとうございました!

答えて

1

理想的にデータを使用すると、また、ものの数を保持するためにvariable in liquidを作成することができますが、レンダリング前にフィルタリングされなければならない、私はすべての作業を行い、独自のフィルタを作成することです言ったように、理想的なソリューション

{% assign rendered = 0 %} 

{% for study in site.data.studies %} 
    {% if study.category contains "foo" %} 
     <div class="col-sm-3"> 
     <h3>{{ study.title }}</h3> 
     <div class="list-of-attributes"> 

      <h6>attributes: </h6> 
      {{ study.attributes }} 
     </div> 
     </div> 

    {% assign rendered = rendered | plus: 1 %} 
    {% if rendered == 4 %} 
     {% break %} 
    {% endif %} 

    {% endif %} 
{% endfor %} 

をレンダリング(カテゴリ別にフィルタと結果の数を制限する)

{% assign filtered = site.data.studies | my_custom_filter %} 
{% for study in filtered %} 
    <div class="col-sm-3"> 
    <h3>{{ study.title }}</h3> 
    <div class="list-of-attributes"> 
     <h6>attributes: </h6> 
     {{ study.attributes }} 
    </div> 
    </div> 
{% endfor %} 
+1

を| +%1}}は動作しない '{%assign rendered = rendered + 1%} 'よりも優れています。 –

+0

提案してくれてありがとう@DavidJacquel! –

+0

これは素晴らしいです、ありがとう!私は似たようなことを試していましたが、それほど得意ではありませんでした。そしてあなたの解決策を見て、私が間違っていたことを理解しています。とても感謝しております! – avp

0

あなたcategoryが文字列ではなく、あなたがすることができる配列であることを仮定:

{% assign selected = site.data.studies | where: 'category','foo' %} 
{% for study in selected limit:4 %} 
    <div class="col-sm-3"> 
    <h3>{{ study.title }}</h3> 
    <div class="list-of-attributes"> 
     <h6>Attributes: </h6> 
     {{ study.attributes }} 
    </div> 
    </div> 
{% endfor %} 

そして、あなたのcategoryは、あなたがこのようなジキル3.2 where_expフィルタを使用することができます"foo, bar, baz"かと文字列の配列のような文字列である場合:私は `{%アサインがレンダリング=レンダリングだと思う

{% assign selected = site.data.studies | where_exp:"item", "item.category contains 'foo'" %} 
+0

申し訳ありませんが、データファイルのカテゴリに複数の文字列が含まれているため、「foo、bar、unicorn」と表示されたり、「bar、unicorn、foo」が表示される可能性があります。ですから、変数を設定してからそれを引っ張るのではなく、 'contains'を使う必要がありました。でもありがとう! – avp

関連する問題