Skip to content
alabid edited this page Aug 10, 2012 · 3 revisions

Template filters are helper functions that are available to every view template during the mako template pre-processing stage. Below, I'll show some of helper functions available for use and how to use it in a mako template.

Template Filter Methods

Note that all the methods described below are static methods and not class methods. So you don't need to instantiate the Filter class at any time to use the methods described below.

Method : Filters.val

Params : value. Value of any data type

Returns : None if value is any of the following: None, '', 'null', 'undefined', {}. Else returns the value.

Example Use

${"something" if Filters.val("") else "nothing"} # outputs nothing

Method : Filters.version

Params : None

Returns : version of the system as defined in config/settings.py. Especially useful for cache busting.

Example Use

 <script type="text/javascript" src="some-js-file.js?${Filters.version()}"></script>

Method : Filters.str

Params : value - to convert to string

Returns : '' (empty string) for '', None, [], {}. Returns JSON string if value is a dictionary. Adds commas to numbers when necessary. Else returns the string representation of value.

Example Use

 <script type="text/javascript" src="some-js-file.js?${Filters.version()}"></script>

Method : Filters.is_true

Params : value - to check if true

Returns : Returns True if value is True, 'true', or 'on'. Else returns Fals

Example Use

 % if Filters.is_true('true')
    <span>It's true!</span>
 % endif

Method : Filters.strip_html

Params : value - to strip html from

Returns : Strips all html (+xml) tags from value and returns the result.

Example Use

${Filters.strip_html("<span>This is <b>what I'm</b> talking about</span>")}
<!-- This is what I'm talking about -->  

Method : Filters.long_timestamp

Params :

  • dt_str - date string
  • tz (default - "America/New_York") - time zone

Returns : A more detailed representation of the date string (dt_str) according to the timezone tz

Example Use

${Filters.long_timestamp("Fri Aug 10 2012 15:30:02 GMT-0400 (EDT)")}

Method : Filters.short_timestamp

Params* :

  • dt_str - date string
  • tz (default - "American/New_York") - time zone

Returns : Returns a date in the form month/day/year hour:minute.


Method : Filters.short_date

Params :

  • dt_str - date string
  • tz (default - "American/New_York") - time zone

Returns : Returns a date in the form month/day/year


Method : Filters.ellipsis

Params :

  • data - string
  • limit - number limit
  • append (default - "...") - string to use to mark ellipsis

Returns : Returns data back if length of data is less than limit. Else truncates data by (data length + append length) - limit


Method : Filters.to_json

Params : dict - dictionary to convert to JSON

Returns : JSON string representation of dict


Method : Filters.idize

Params : string - string to cleanse/idize

Returns* : Replaces all non-alphnumeric characters in string with _ and returns the result in lowercase


Method : url_pretty

Params : string - url string to make pretty

Returns : None on '' (empty string). If string has more than 32 characters, it returns the first 32 characters. Also replaces all non-alphanumeric characters with _


Method : Filters.add_commas

Params :

  • val - string to add commas to (should be a string that represents some number - either int or float)
  • as_data_type (defaults to 'int') - either 'int' or 'float'
  • the_locale (defaults to locale.LC_ALL) - the locale to associate with the string number

Returns : Delimits 000 in the integer part of val with commas.

Example Use

${Filters.add_commas("20000")} 
<!-- produces "20,000" -->

Method : Filters.pluralize

Params : string - string to pluralize

Returns : The pluralized form of string

Example Use

${Filters.pluralize("class")}
<!-- produces "classes" -->

Method : Filters.dict_get

Params :

  • dict - the dictionary to retrieve an item from
  • key - string of keys to use to retrieve item from dict
  • default (default value is None) - what to return if error encountered

Example Use

# Assume to_get is:
# to_get = { 'key1'  : {
#              'subkey' : [{'subsubkey1' : 9}, {}]
#            }
#          }

${Filters.dict_get(to_get, 'key1.subkey.0.subkey1')}
<!-- returns 9 -->

Clone this wiki locally