diff --git a/_filters/sum.md b/_filters/sum.md new file mode 100644 index 00000000..54205d75 --- /dev/null +++ b/_filters/sum.md @@ -0,0 +1,42 @@ +--- +title: sum +description: Liquid filter that sums all items in an array. +--- + +Sums all items in an array. + +If a string is passed as an argument, it sums the property values. + +In this example, assume the object `collection.products` contains a list of products, and each `product` object has a `quantity` property. Using `assign` with the `sum` filter creates a variable that contains the total quantity for all products in the collection. + +
Input
+```liquid +{%- raw -%} +{% assign total_quantity = collection.products | sum: "quantity" %} + +{{ total_quantity }} +{% endraw %} +``` + +Output
+```text +6 +``` + +The `sum` filter also works without any argument. + +In this example, assume the object `article.ratings` is an array of integers. Using `assign` with the `sum` filter creates a variable that contains the total ratings for the article. + +Input
+```liquid +{%- raw -%} +{% assign total_rating = article.ratings | sum %} + +{{ total_rating }} +{% endraw %} +``` + +Output
+```text +6 +```