Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Latest commit

 

History

History
279 lines (220 loc) · 9.27 KB

filters.md

File metadata and controls

279 lines (220 loc) · 9.27 KB

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.

Built-In Filters

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.

Arguments

  1. value (mixed) The value to add to the variable before printing it to the page. Accepts any array, object, number, and string.

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.

Arguments

  1. format (string) The format to convert the date to.
Date formatting character information from php.net/date.
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

default(value)

If the variable is undefined, null, or false, a default return value can be specified.

Arguments

  1. value (mixed) Fallback value if the variable is falsy.

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.

Arguments

  1. 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 value.

lower

Return the variable in all lowercase letters.

replace(search, replace[, flags])

Uses built-in JavaScript replace method. Provide a regular-expression or a string and a replacement string.

Arguments

  1. search (string) string converted to a regular expression. Example: '\s' will become /\s/, while 's' will become /s/
  2. replace (string) a string to replace the matched parts from search
  3. 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