Array Filters
These are provided in the default filters. Specifically, when TemplateOptions
is constructed, these are added via the extension method WithArrayFilters
.
join
Joins the elements of an array into a string. Basically proxies string.Join
. The method will call ToStringValue
on everything in the array, so if they’re not already strings, they’ll get “flattened” into strings.
{{ "Alec,Gabrielle,Isabella" | split:"," | join: " + " }}
Alec + Gabrielle + Isabella
first
Returns the first element of an array. This just calls the “first” virtual member on the FuildValue
input. So it’s not using LINQ or anything behind the scenes. And you can call it on anything, but it will return NilValue
for anything except an array.
{% assign children = "Alec,Gabrielle,Isabella" | split:"," %}
{{ children | first }}
Alec
If you need any other information from the returned element, it’s probably easier to just call the property yourself. This does exactly the same thing.
{{ children.first.age }}
last
Returns the last element of an array.
This just calls the “last” virtual member on the FuildValue
input, so all the same information provided for “first” (above) applies.
{% assign children = "Alec,Gabrielle,Isabella" | split:"," %}
{{ children | last }}
Isabella
concat
Glues two arrays together.
Internally, it creates a new List<FluidValue>
, adds all the original elements to it, then adds all the first argument’s elements to it.
{% assign children = "Alec,Gabrielle,Isabella" | split:"," %}
{% assign pets = "Jersey,Turks,Caicos" | split:"," %}
{{ children | concat:pets | join: ", " }}
Alec, Gabrielle, Isabella, Jersey, Turks, Caicos.
map
Extracts a single expression from all array elements into a new array.
It’s the rough equivalent of Select
in LINQ.
context.SetValue("children", new List<Person>() {
new Person() { Name = "Alec", Age = 27 },
new Person() { Name = "Gabrielle", Age = 20 },
new Person() { Name = "Isabella", Age = 17 }
});
{{ children | map:"Age" | join:", " }}
27, 20, 17
While the expression will usually be a property (as above), internally the filter is calling GetValue
on the FluidValue
, so it can be anything that will work with that, including virtual member.
For example, if we were dealing with an array of strings:
{% assign children = "Alec,Gabrielle,Isabella" | split:"," %}
{{ children | map:"size" | join:", " }}
4, 9, 8
reverse
When called on an array, it returns a new array with the elements in the reverse order.
{{ children | reverse | join:", " }}
Isabella, Gabrielle, Alec
When called on a string, it returns an array of the characters in reverse order. If you output this, it will appear to be a string, but you can actually treat it as an array.
{{ "Deane" | reverse }}.
{{ "Deane" | reverse | join: ", " }}.
enaeD
e, n, a, e, D
size
Calls the size
property on the array value.
{{ children | size }}
3
It’s exactly the same as this:
{{ children.size }}
sort
ArrayFilters.Sort
Returns a new array, sorted either by a supplied expression, or – if you don’t pass an expression in – by whatever the element returns from ToStringValue()
.
context.SetValue("children", new List<Person>() {
new Person() { Name = "Alec", Age = 27 },
new Person() { Name = "Gabrielle", Age = 20 },
new Person() { Name = "Isabella", Age = 17 }
});
{{ children | sort:"Age" | map:"Name" | join:", " }}
Isabella, Gabrielle, Alec
sort_natural
It’s the same as sort
, but when you don’t pass in any arguments, the results of the ToStringValue
are case insensitive.
uniq
Returns a new array without duplicate values.
Internally it’s calling the Distinct
LINQ function.
{% comment %} My youngest is very loud, so she counts twice... {% endcomment %}
{% assign children = "Alec,Gabrielle,Isabella,Isabella" | split:"," %}
{{ children | uniq | join:", " }}
Alec, Gabrielle, Isabella
where
Filters an array by comparing a member to either (1) a matching value or (2) a boolean true
.
This will return an array of just the people with a Name
of “Bob”:
{{ people | where:"Name","Bob" | map:"Age" | join:", " }}
This will return an array of just the people who like toasters – where IsToasterphile
is a boolean true
{{ people | where:"IsToasterphile" | map:"Name" | join:", " }}