我的理解是,Liquid将Ruby哈希转换为数组,以便在标记中使用。例如,使用Jekyll时:
{% for category in site.categories %}
<li>{{ category[0] }}</li>
{% endfor %}..。将site.categories转换为元组数组,其中引用键,1值列表。
如果我想让上面的类别图按键(每个元组的)按字母顺序排序,我该怎么做呢?
发布于 2011-06-20 02:01:11
默认的Liquid实现和Jekyll所做的添加都不能满足您的需求。
恐怕您想要的东西在目前的设置下是不可能实现的。您必须使用monkeypatch Jekyll或Liquid,才能使散列以排序的顺序返回它们的键。
发布于 2011-11-25 08:10:17
这是一个古老的问题,但我只是花了最后一点时间自己解决这个问题。我使用以下代码实现了您(和我)想要的结果。
{% capture get_items %}
{% for cat in site.categories %}
{{ cat | first }}
{% endfor %}
{% endcapture %}
{% capture num_words %}
{{ get_items | split:' ' | sort | join:' ' | number_of_words }}
{% endcapture %}
{% for item in (1..num_words) %}
<li>{{ get_items | split:' ' | sort | join:' ' | truncatewords:item | remove:'. ..' | split:' ' | last }}</li>
{% endfor %}发布于 2012-08-18 20:41:37
您也可以使用数组而不是散列!
不使用这个yaml:
categories:
a_category: category description
another_category: another category description你可以使用这个:
categories:
- {name: 'a category', description: 'category description'}
- {name: 'another category', description: 'another category description'}然后,您可以像这样迭代,顺序将保持不变:)
{% for category in site.categories %}
<li>{{ category['name'] }}</li>
{% endfor %}https://stackoverflow.com/questions/6387540
复制相似问题