-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
712949b
commit 4a77efe
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
<p class="code-label">Input</p> | ||
```liquid | ||
{%- raw -%} | ||
{% assign total_quantity = collection.products | sum: "quantity" %} | ||
{{ total_quantity }} | ||
{% endraw %} | ||
``` | ||
|
||
<p class="code-label">Output</p> | ||
```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. | ||
|
||
<p class="code-label">Input</p> | ||
```liquid | ||
{%- raw -%} | ||
{% assign total_rating = article.ratings | sum %} | ||
{{ total_rating }} | ||
{% endraw %} | ||
``` | ||
|
||
<p class="code-label">Output</p> | ||
```text | ||
6 | ||
``` |