Variable Filters #
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.
{{ name|title }} was born on {{ birthday|date('F jS, Y') }} and has {{ bikes|length|default("zero") }} bikes.
Filters can also be applied to blocks of content directly with the filter tag.
{% filter uppercase %}oh hi, paul{% endfilter %}
Built-In Filters #\
- add
- addslashes
- capitalize
- date
- default
- e
- escape
- first
- join
- json_encode
- last
- length
- lower
- raw
- replace
- reverse
- striptags
- title
- uniq
- upper
- url_encode
- url_decode
add(value) #
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
.
addslashes #
Returns a string with backslashes in front of characters that need to be quoted for database queries, etc.
- single quote
'
- double quote
"
- backslash
\
capitalize #
Capitalize the first character in the string.
date(format) #
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. See the date formatting table.
- tzOffset (number) The timezone offset in minutes from GMT to display the date in.
- For example, if your server is running in PDT, but you want to display your dates in CDT, set this to 360.
- This can also be set globally via config variable
tzOffset
on swig.init.
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) |
z |
The day of the year starting from 0 | 0 - 365 (January 1st - December 31st [only on leap year]) |
Week | ||
W |
ISO-8601 week number of the year | 42 |
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 |
t |
Number of days in the given month | 28 - 31 |
Year | ||
L |
Whether or not the given year is a leap year. | true or false |
o |
ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. | 1999 |
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 |
B |
Swatch Internet Time | 000 - 999 |
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 | ||
c |
ISO 8601 formatted date | 2011-09-06T16:05:02.000Z |
r |
RFC 2822 formatted date | Tue, 06 Sep 2011 16:05:02 GMT |
U |
Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | 1315690513 |
default(value) #
If the variable is undefined
, null
, or false
, a default return value can be specified.
- value (mixed) Fallback value if the variable is falsy.
escape([type]) / e([type]) #
Force escape the output of the variable. Optionally use e
as a shortcut filter name. This filter will be applied by default if autoescape is turned on.
Character | Replacement |
---|---|
& | & |
< | < |
> | > |
" | " |
' | ' |
Pass js
as the type to your escape filter to toggle escaping as JavaScript.
Character | Replacement |
---|---|
ASCII charCodes less than 32 ESC, TAB, SPACE, etc… |
\u0001 - \u001F |
& | \u0026 |
< | \u003C |
> | \u003E |
' | \u0027 |
" | \u0022 |
= | \u003D |
- | \u002D |
; | \u003B |
- type (string) optional passing "js" as the type will force JavaScript-safe escaping.
first #
Returns the first element of an array. Uses underscore.js first
join(glue) #
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.
json_encode #
Return a JSON string of the variable.
last #
Returns the last element of an array. Uses underscore.js last
length #
Return the length
property of the variable. If the variable is an object, this will return the length of the keys on the object.
lower #
Return the variable in all lowercase letters.
raw #
Do not escape the output of the variable.
replace(search, replace[, flags]) #
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]
reverse #
If the value is an Array, this filter will reverse all items in the array.
striptags #
Strip all HTML/XML tags.
title #
Change the output to title case–the first letter of every word will uppercase, while all the rest will be lowercase.
uniq #
Produces a duplicate-free version of the array. Uses underscore.js uniq
upper #
Return the variable in all uppercase letters
url_encode #
Encode a URI component.
url_decode #
Decode a URI component.
Writing Custom Filters #
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