harrydeluxe / php-liquid

A PHP port of Ruby's Liquid Templates
http://www.delacap.com/artikel/Liquid-Templates/
MIT License
239 stars 119 forks source link

For - loop - else #19

Open ghost opened 11 years ago

ghost commented 11 years ago

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

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

chrstphrknwtn commented 11 years ago

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 %}
ghost commented 11 years ago

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 notifications@github.com 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.

chrstphrknwtn commented 11 years ago

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