0
我有我的模板下面的代碼:訪問與傑基爾/液體模板變量數組
{% for post in site.posts %}
{% capture num_colors %}{{ site.colors | size }}{% endcapture %}
{% capture color_index %}{{ forloop.index0 | mod: num_colors }}{% endcapture %}
<a href="{{ post.url }}" class="post-box" rel="bookmark" title="{{ post.title }}">
<div class="post-block {{ site.colors[color_index] }}">
<div class="contents">
<div class="cat-tag">
{{ post.categories[0] | upcase }}
</div>
<h2>{{ post.title }}</h2>
</div>
</div>
</a>
{% endfor %}
這使返回什麼:即使num_colors
,color_index
,並site.colors
{{ site.colors[color_index] }}
都將返回正確的事當我嘗試打印它們時。
顏色在我_config.yml定義爲:
colors: [light_blue, coral, yellow, teal, blue, deep_blue]
我使用一個插件來獲得模量。基本上我只想爲每個帖子添加一個類,當它超過顏色的總數時將重新開始。這看起來很簡單,所以我很困惑。
我想這是因爲'capture'在變量捕獲文本。這可能不適用於數組訪問。你可以嘗試'分配'索引?但這只是一個猜測,我無法在答案中自己嘗試一下。 – michaPau
@michaPau做到了!添加答案,我會接受它 – Evan