Pregunta

If there is any way to check for odd or even in array.size, for example?

{% if.collections.size == EVEN %}

{% endif %}
¿Fue útil?

Solución

Refer to the Maths Modulo filter: http://docs.shopify.com/themes/liquid-basics/output#modulo

{% assign value = collections.size | modulo:2 %}
{% if value == 0 %}
  even
{% else %}
  odd
{% endif %}

Or if you'd prefer a one liner:

{{ collections.size | modulo:2 | plus:1 | pluralize:'even','odd' }}

Otros consejos

after some experiments, I found the solution, maybe for anyone will be helpfull

   {% assign even = false %}
  {% for collection in collections %}
   {% capture u %}{% cycle 'odd', 'even' %}{% endcapture %}
  {% if u == 'even' %}
  {% assign even = true %}
  {{u}}
  {% endif %}
  {% endfor %}

  {% if even == true %}
  collection-even
  {% endif %}
int arr[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "The Even no are : \n";
for (int i = 1; i <= 10; i++) // for start for only i....(even nos)
{
    if (i % 2 == 0)
    {
        cout << i;
        cout << " ";
    }
}
cout << "\nThe Odd no are : \n";
for (int j = 1; j <= 10; j++) // for start for only j....(odd nos)
{
    if (j % 2 != 0)
    {
        cout << j;
        cout << " ";
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top