Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
https://shopify.github.io/liquid/
MIT License
11.15k stars 1.4k forks source link

A filter to compute modulus/remainder? #124

Closed cboettig closed 12 years ago

cboettig commented 12 years ago

Liquid has been a fantastic library with very useful filters. One that I keep feeling that lack of is a modulus/remainder operator, which comes in real handy when computing row/column positions for where to stick certain elements on a grid.

For instance, I often have an array of content, maybe it's site.posts on a jekyll site, and I want to organize them into a twitter-bootstrap style grid.

This is easy to specify column-wise in liquid, since you have integer division

{% capture n  %}{{ site.posts | size | divided_by:3}}{% endcapture %}
{% capture m %} {{ n | times:2 }}{% endcapture %}

<div class="span3">
{% for post in site.posts limit:n %}
{{ post | content }}
{% endfor %}
</div>

<div class="span3">
{% for post in site.posts limit:n offset:n%}
{{ post | content }}
{% endfor %}
</div>

<div class="span3">
{% for post in site.posts limit:n offset:m %}
{{ post | content }}
{% endfor %}
</div>

But if I'd rather fill out posts going across rows instead of down columns I need to do get the remainder of index/3, not the integer divisor. Think you might consider adding support for such a filter? (guess I should learn to extend my own filters too...)

titanous commented 12 years ago

This was implemented in c4d713b6bbb1a61f65583de32112756200231d5e.

cboettig commented 12 years ago

Brilliant, thanks. I'll add it to the wiki. Is there an automated list of all filters available in the documentation somewhere? Perhaps I shouldn't be relying on the wiki to know what's implemented...