Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ul>
- <!--selecting the category of the products for testing purposes-->
- {%- assign selected_category = "electronics" %}
- <!--sorting products by price-->
- {%- assign sorted_products = products | sort: "price" %}
- {%- assign category_found = false %}
- <h2>Products Category: {{ selected_category | capitalize }}</h2>
- <!--showing products by selected category-->
- {%- for product in sorted_products %}
- {%- if selected_category == "" or product.category == selected_category %}
- {%- assign category_found = true %}
- <li>
- <!--showing product name and price like in the output example/missing price handling-->
- <a href="{{ product.name | prepend: "#" }}">
- {%- if product.price != null %}
- ${{ product.price | round: 2 }}
- {%- else %}
- Price unavailable
- {%- endif %}
- </a>
- </li>
- {%- endif %}
- {%- endfor %}
- <!--edge case handling-->
- {%- if category_found == false %}
- <h2>No products were found in this category.
- All our available products: </h2>
- {%- for product in sorted_products %}
- <li>
- <a href="{{ product.name | prepend: "#" }}">
- {%- if product.price != null %}
- ${{ product.price | round: 2 }}
- {%- else %}
- Price unavailable
- {%- endif %}
- </a>
- </li>
- {%- endfor %}
- {%- endif %}
- </ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement