Used to modify variables. Filters are added directly after variable names, separated by the pipe (|) character. You can chain multiple filters together, applying one after the other in succession.
Adds the value to the variable. Strings that can be converted to integers will be summed, not concatenated, as in the example below.
- value (mixed) The value to add to the variable before printing it to the page. Accepts any
array
,object
,number
, andstring
.
Returns a string with backslashes in front of characters that need to be quoted for database queries, etc.
- single quote
'
- double quote
"
- backslash
\
Capitalize the first character in the string.
Convert a valid date into a format as specified. Mostly conforms to php.net's date formatting.
- format (string) The format to convert the date to.
Format Character | Description | Example Output |
---|---|---|
Day | ||
d |
Day of the month, 2 digits with leading zeros | 01 - 31 |
D |
A textual representation of a day, three letters | Mon - Sun |
j |
Day of the month without leading zeros | 1 - 31 |
l |
A full textual representation of the day of the week | Monday - Sunday |
N |
ISO-8601 numeric representation of the day of the week | 1 - 7 (Monday - Sunday) |
S |
English ordinal suffix for the day of the month, 2 characters | st, nd, rd, or th |
w |
Numeric representation of the day of the week | 0 - 6 (Sunday - Saturday) |
Month | ||
F |
A full textual representation of a month, such as January or March | January - December |
m |
Numeric representation of a month, with leading zeros | 12 - 01 |
M |
A short textual representation of a month, three letters | Jan - Dec |
n |
Numeric representation of a month, without leading zeros | 1 - 12 |
Year | ||
Y |
A full numeric representation of a year, 4 digits | 1999 |
y |
A two digit representation of a year | 99 |
Time | ||
a |
Lowercase Ante meridiem and Post meridiem | am or pm |
A |
Uppercase Ante meridiem and Post meridiem | AM or PM |
g |
12-hour format of an hour without leading zeros | 1 - 12 |
G |
24-hour format of an hour without leading zeros | 0 - 23 |
h |
12-hour format of an hour with leading zeros | 01 - 12 |
H |
24-hour format of an hour with leading zeros | 00 - 23 |
i |
Minutes with leading zeros | 00 - 59 |
s |
Seconds, with leading zeros | 00 - 59 |
Timezone | ||
O |
Difference to Greenwich time (GMT) in hours | +0200 |
Z |
Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | -43200 - 50400 |
Full Date & Time | ||
r |
RFC 2822 formatted date | Sat, 10 Sep 2011 14:34:30 -0700 |
U |
Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | 1315690513 |
If the variable is undefined
, null
, or false
, a default return value can be specified.
- value (mixed) Fallback value if the variable is falsy.
Returns the first element of an array. Uses underscore.js first
If the value is an Array, you can join each value with a delimiter and return it as a string.
- glue (string) Concatenation string to join each item in the array with.
Return a JSON string of the variable.
Returns the last element of an array. Uses underscore.js last
Return the length
property of the value.
Return the variable in all lowercase letters.
Uses built-in JavaScript replace method. Provide a regular-expression or a string and a replacement string.
- search (string) string converted to a regular expression. Example:
'\s'
will become/\s/
, while's'
will become/s/
- replace (string) a string to replace the matched parts from search
- flags (string) optional Regular expression flags. [reference]
If the value is an Array, this filter will reverse all items in the array.
Strip all HTML/XML tags.
Change the output to title case–the first letter of every word will uppercase, while all the rest will be lowercase.
Produces a duplicate-free version of the array. Uses underscore.js uniq
Return the variable in all uppercase letters
Encode a URI component.
Decode a URI component.
Custom filters are very easy to write for your own project that uses Swig.
Create a file called myfilters.js
and include it in the Swig init function:
swig.init({ filters: require('myfilters') });
In your myfilters.js
file, each filter method is just a simple javascript method. For example, here's a filter to make a variable all lowercase:
exports.lower = function (input) {
return input.toString().toLowerCase();
};
For more examples, view the filters.js source file