Refactor conditional liquid variable

8 Views Asked by At

I want to reverse (again) the limited collection only when the variable include.limit is a value.

I am unclear if liquid filters can be conditionally called, or if first line of a for loop can be conditionally switched... I found one working solution, which is more verbose than I would like. Does anyone have a better solution?

The below solution works, but I am unhappy about it.

{% assign sorted_posts = site.events | filter_tags: include.tags | reverse %}
{% if include.limit %}  
  {% for post in sorted_posts reversed limit: include.limit %}
      // same code  
  {% endfor %}
{% else %} 
  {% for post in sorted_posts %}
     // same code
  {% endfor %} 
{% endif %} 

I want to reverse (again) the limited array only when it is limited.

{% assign sorted_posts = site.events | filter_tags: include.tags | reverse %}
{% if include.limit %}  
  {% for post in sorted_posts reversed limit: include.limit %}
{% else %} 
  {% for post in sorted_posts %}
{% endif %}
  // code called once  
{% endfor %}

0

There are 0 best solutions below