From 8845d1ada1d6a42d4d1dbabc9e906fe2a33eb88e Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 12 Oct 2018 16:19:19 -0400 Subject: [PATCH 1/3] Add documentation for where filter --- filters/where.md | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 filters/where.md diff --git a/filters/where.md b/filters/where.md new file mode 100644 index 00000000..06c12ff8 --- /dev/null +++ b/filters/where.md @@ -0,0 +1,90 @@ +--- +title: where +description: Liquid filter that selects from arrays. +--- + +Creates an array including only the objects with a given property value, or any [truthy]({{ "/basics/truthy-and-falsy/truthy" | prepend: site.baseurl }}) value by default. + +In this example, assume you have a list of products and you want to show your kitchen products separately. Using `where`, you can create an array containing only the products that have a `"type"` of `"kitchen"`. + +

Input

+{% raw %} +```liquid +All products: +{% for product in products %} +- {{ product.title }} +{% endfor %} + +{% assign kitchen_products = site.pages | where: "type", "kitchen" %} + +Kitchen products: +{% for product in kitchen_products %} +- {{ product.title }} +{% endfor %} +``` +{% endraw %} + +

Output

+```text +All products: +- Vacuum +- Spatula +- Television +- Garlic press + +Kitchen products: +- Spatula +- Garlic press + +``` + +Say instead you have a list of products and you only want to show those that are available to buy. You can `where` with a property name but no target value to include all products with a [truthy]({{ "/basics/truthy-and-falsy/truthy" | prepend: site.baseurl }}) `"available"` value. + +

Input

+{% raw %} +```liquid +All products: +{% for product in products %} +- {% product.title %} +{% endfor %} + +{% assign available_products = products | where: "available" %} + +Available products: +{% for product in available_products %} +- {% product.title %} +{% endfor %} + +``` +{% endraw %} + +

Output

+ +```text +All products: +- Coffee mug +- Limited edition sneakers +- Boring sneakers + +Available products: +- Coffee mug +- Boring sneakers + +``` + + +The `where` filter can also be used to find a single object in an array when combined with the `first` filter. For example, say you want to show off the shirt in your new fall collection. + +

Input

+{% raw %} +```liquid +{% assign new_shirt = products | where: "type", "shirt" | first %} + +Featured product: {% new_shirt.title %} +``` +{% endraw %} + +

Output

+```text +Featured product: Hawaiian print sweater vest +``` \ No newline at end of file From ea0bf2d30b86bc12d4097e1368c184e418e0db3f Mon Sep 17 00:00:00 2001 From: Samuel Date: Fri, 19 Oct 2018 10:52:36 -0400 Subject: [PATCH 2/3] fix braces --- filters/where.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/filters/where.md b/filters/where.md index 06c12ff8..d58adbbf 100644 --- a/filters/where.md +++ b/filters/where.md @@ -3,7 +3,7 @@ title: where description: Liquid filter that selects from arrays. --- -Creates an array including only the objects with a given property value, or any [truthy]({{ "/basics/truthy-and-falsy/truthy" | prepend: site.baseurl }}) value by default. +Creates an array including only the objects with a given property value, or any [truthy]({{ "/basics/truthy-and-falsy#truthy" | prepend: site.baseurl }}) value by default. In this example, assume you have a list of products and you want to show your kitchen products separately. Using `where`, you can create an array containing only the products that have a `"type"` of `"kitchen"`. @@ -38,21 +38,21 @@ Kitchen products: ``` -Say instead you have a list of products and you only want to show those that are available to buy. You can `where` with a property name but no target value to include all products with a [truthy]({{ "/basics/truthy-and-falsy/truthy" | prepend: site.baseurl }}) `"available"` value. +Say instead you have a list of products and you only want to show those that are available to buy. You can `where` with a property name but no target value to include all products with a [truthy]({{ "/basics/truthy-and-falsy#truthy" | prepend: site.baseurl }}) `"available"` value.

Input

{% raw %} ```liquid All products: {% for product in products %} -- {% product.title %} +- {{ product.title }} {% endfor %} {% assign available_products = products | where: "available" %} Available products: {% for product in available_products %} -- {% product.title %} +- {{ product.title }} {% endfor %} ``` @@ -80,7 +80,7 @@ The `where` filter can also be used to find a single object in an array when com ```liquid {% assign new_shirt = products | where: "type", "shirt" | first %} -Featured product: {% new_shirt.title %} +Featured product: {{ new_shirt.title }} ``` {% endraw %} From fbad7cf377b712783d6de33e8cd8da841c5e0db7 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 7 Jan 2019 13:13:02 -0500 Subject: [PATCH 3/3] Fix variable name --- filters/where.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/filters/where.md b/filters/where.md index d58adbbf..178f8293 100644 --- a/filters/where.md +++ b/filters/where.md @@ -15,7 +15,7 @@ All products: - {{ product.title }} {% endfor %} -{% assign kitchen_products = site.pages | where: "type", "kitchen" %} +{% assign kitchen_products = products | where: "type", "kitchen" %} Kitchen products: {% for product in kitchen_products %} @@ -87,4 +87,4 @@ Featured product: {{ new_shirt.title }}

Output

```text Featured product: Hawaiian print sweater vest -``` \ No newline at end of file +```