php-liquid icon indicating copy to clipboard operation
php-liquid copied to clipboard

For - loop - else

Open ghost opened this issue 11 years ago • 3 comments

{% for product in product %} {{ product.name }} {% else %} No products found {% endfor %}

does not work... would be great if could have that!

ghost avatar Feb 24 '13 04:02 ghost

Your logic is broken, to acheive what you want you need to have an if statement for else to work. You need to have something like:

if there are products, then for each product show some product information else tell me there are no products

In liquid (based on shopify), it could look like this:

{% if collection.products.size > 0 %}
  {% for product in collection.products %}
    {{ product.name }}
  {% endfor %}
{% else %}
  No products found
{% endif %}

chrstphrknwtn avatar Apr 24 '13 10:04 chrstphrknwtn

Well.. Since this is based on liquid it should work... I understand there is a more correct way of doing it.. But in the original liquid you can do it like i said without making an if statement

On 24 apr 2013, at 18:41, chrstphrknwtn [email protected] wrote:

Your logic is broken, to acheive what you want you need to have an if statement for else to work. You need to have something like:

if there are products, then for each product show some product information else tell me there are no products

In liquid (based on shopify), it could look like this:

{% if collection.products.size > 0 %} {% for product in collection.products %} {{ product.name }} {% end for %} {% else %} No products found {% endif %} — Reply to this email directly or view it on GitHub.

ghost avatar Apr 24 '13 11:04 ghost

sorry, my mistake... the way you describe is indeed correct, and insomuch a bug for this php port.

chrstphrknwtn avatar Apr 24 '13 11:04 chrstphrknwtn