Miscellaneous Filters
These are provided in the default filters. Specifically, when TemplateOptions
is constructed, these are added via the extension method WithMiscFilters
.
default
Returns a “default value” if the input is not invalid, which means a NilValue
, an EmptyValue
, or a boolean false
.
The default value is the first argument.
{{ "" | default:"Deane" }}
{{ "Annie" | default:"Deane" }}
{{ false | default:"Deane" }}
Deane
Annie
Deane
You can pass in a named argument of allow_false:true
and boolean false
values will be considered valid.
date
Formats a date.
This uses the Liquid syntax, not the C# syntax.
If you want to use C# formatting syntax (which will not be Liquid-compliant), use format_date
(see below).
Here is a table of the Liquid date formatting.
The input must parse as a DateTime
, and the argument is required.
{{ "2021-12-09" | date:"%A, %B %e, %Y" }}
Thursday, December 9, 2021
(Note the day represented by %e
is space-padded, hence the extra space after the month.)
There are some handy “roll-ups”:
%c
= “Thursday, 09 December 2021 00:00:00” (note that example input had no time specified)%D
= “12/09/21”%F
= “2021-12-09”%s
= “1639029600” (number of seconds since the epoch, which is helpful for JavaScript)
raw
MiscFilters.Raw
compact
Removes “nil” values from arrays.
Specifically, it iterates the input array, calls IsNil()
on all values, and returns a new array of only those values which returned false
At the default installation, this is only NilValue
, BlankValue
, and EmptyValue
.
Everything other value – including empty strings and boolean false
values – will return false
from IsNil()
which is the default non-overridden behavior from the FluidValue
base class (outside of the three mentioned above, none of the other derived classes override the method).
url_encode
Encodes special characters in strings according to standard URL encoding. (RFCs 3986 and 3987)
{{ "/page?a=b" | url_encode }}
%2Fpage%3Fa%3Db
url_decode
Decodes encoded special characters to their ASCII intentions.
{{ "%2Fpage%3Fa%3Db" | url_decode }}
/page?a=b
base64_encode
Base64 encodes a string. Empty strings will
{{ "Deane" | base64_encode }}
RGVhbmU=
base64_decode
Base64 decodes a string.
{{ "RGVhbmU=" | base64_decode }}
Deane
base64_url_safe_encode
Same as base64_decode
but it replaces +
with -
and /
with _
so the value can be safely passed in a URL.
base64_url_safe_decode
Decodes a value created by base64_url_safe_decode
.
strip_html
Removes anything between <
and >
, inclusive.
{{ "<p>Deane</p>" | strip_html }}
Deane
It doesn’t parse or lex the HTML, it literally just iterates all the characters and concatenates anything outside of brackets.
escape
Escapes HTML – converts brackets to entities.
{{ '<p>Deane</p>' | escape }}
<p>Deane</p>
escape_once
MiscFilters.EscapeOnce
handle / handleize
MiscFilters.Handleize
json
MiscFilters.Json
Serializes the object to JSON.
Works with all FluidTypes
.
An array will serialize each individual element.
people.Add(new Person() { FirstName = "Deane", LastName = "Barker" });
people.Add(new Person() { FirstName = "Deane", LastName = "Annie" });
c.SetValue("people", people);
{{ people | json }}
[{"FirstName":"Deane","LastName":"Barker"},{"FirstName":"Deane","LastName":"Annie"}]
Important Note: when rendering objects, this filter respects member access (see Member Access). If you’re restricting access to members, this might return a bunch of empty values.
time_zone
MiscFilters.ChangeTimeZone
format_number
MiscFilters.FormatNumber
format_string
MiscFilters.FormatString
Allows for token replacement within a string. It wraps the String.Format
method in C#.
{{ "Vehicle: {0}, Wheels: {1}" | format_string:"bicycle", 2 }}
{{ "Vehicle: {0}, Wheels: {1}" | format_string:"car", 4 }}
Vehicle: bicycle, Wheels: 2
Vehicle: car, Wheels: 4
You can set the culture with an optional named parameter of culture
.
(Honestly, this one feels weird to me. Fluid is a templating language. Why do we need another way to template a string?)
format_date
This is the C# equivalent of date
(see above). This does use the standard C# date formatting codes.
{{ '2021-12-09' | format_date:'MMMM d, yyyy' }}
December 9, 2021
md5
Generates a 32-digit MD5 hash of the input string.
{{ "Deane" | md5 }}
8558b56b933bc4d7ba586407732a31e7
sha1
Generates a 40-digit SHA-1 hash of the input string.
86284cbdb4649e52ff1ef657f257e2043e7f73af
{{ "Deane" | sha1 }}
sha256
Generates a 64-digit SHA-256 hash of the input string.
{{ "Deane" | sha256 }}
c784dcfcc1e5231ff7d9ad4de123ee91bec736d806576a825315164c6431e01b