Skip to content
雷宗民 edited this page Jun 28, 2013 · 2 revisions

Filters manipulate the output of variables. Filters look like this {{ variablename | filtername }}. For example:

{{ name | upcase }}

Notes: The filters is not case sensitive.

HTML Tags

  • {{'url' | img_tag: 'alt'}} generate <img> tag
  • {{'url' | script_tag}} Generate <script> tag
  • {{'url' | stylesheet_tag: 'media'}} Generate <link> tag
  • {{'link' | link_to: 'url', 'title'}} Generate <a> tag

Math

  • {{123 | plus: 456}} Add
  • {{123 | minus: 456}} Subtract
  • {{123 | times: 456}} Multiply
  • {{123 | divided_by: 456}} Divide
  • {{1.23 | round: 2}} Round (specify how many places after the decimal)
  • {{1.23 | integer}} Round
  • {{1 | random: 2}} Generate random number such that: 1<=N<2
  • {{N | pluralize: 'item', 'items'}} If N > 1 return items, otherwise item

Strings

  • {{'abc' | append: 'd'}} Append to the end of string
  • {{'abc' | prepend: 'd'}} Prepend to the begining
  • {{'a b c' | camelize}} Combine to one camelized name
  • {{'a b c' | capitalize}} Combine to one capitalized name
  • {{'ABC' | downcase}} To lowercase
  • {{'abc' | upcase}} To uppercase
  • {{'<a>' | escape}} Escape for use in HTML
  • {{'this is a book' | handleize}} Combine to hyphen separated word: 'this-is-a-book'
  • {{'abcabc' | replace_first: 'a', 'x'}} Replace the first occurrence of 'a' with 'x'
  • {{'abcabc' | replace: 'a', 'x'}} Replace all occurrences of 'a' with 'x'
  • {{'abcabc' | remove_first: 'a'}} Remove the first occurrence of 'a'
  • {{'abcabc' | remove: 'a'}} Remove all occurrences of 'a'
  • {{'abc\nabc' | newline_to_br}} Replace all newline characters with <br> tag
  • {{'a-b-c' | split: '-'}} Split the string at each occurrence of '-' (returns an array)
  • {{'abcd' | size}} Return the string length
  • {{'123' | strip_html}} Remove all HTML tags; returns: '123'
  • {{'abc\nabc' | strip_newlines}} Remove all newline characters
  • {{'abcdefg' | truncate: 3}} Return only the first N characters
  • {{'this is a book' | truncatewords: 2}} Return only the first N words
  • {{'abcdef' | reverse}} Reverse the characters in the string
  • {{'ancdefg' | substr: N, M}} Extracts parts of a string, beginning at the character at the specified posistion N, and returns the specified number of characters M.

Date and Time

  • {{0 | timestamp}} Take the current time in milliseconds and add 0
  • {{'now' | date: '%H:%M:%S'}} Format date/time (see syntax reference)

Arrays and Objects

  • {{obj | keys}} Return an array of the object's keys
  • {{array | first}} Return the first element of an array
  • {{array | last}} Return the last element of an array
  • {{array | join: ','}} Join the array's elements into a string
  • {{array | size}} Return the array's length
  • {{obj | json}} Return a JSON string of the object
  • {{obj | get: 'prop'}} Get an item of the Object by property name
  • {{array | reverse}} Reverse the order of the array
  • {{array | map: 'prop'}} Take the specified property of each element in the array, returning a new array
  • {{array | sort: 'desc'}} Sort the array's elements by asc or desc order
  • {{array | sort_by: 'prop', 'desc'}} Sort the array's elements by each element's specified property

Other

  • {{count | pagination: size, currentPage}} Get page count of the items when paginated

>

Clone this wiki locally