Filters


Filters provide a way to slightly modify variables before output.

Syntax: {{variable_name|filter_name:'filter parameter'}}

Outlast Framwork supports all of the filters of the Django template system. These are not documented here yet so please read them on Django’s site.

You can also add your own custom filters or use any of Outlast Framework’s extended filter set, the documentation for which is available below.

|photo

Filter: photo – Returns the url of the photo object. If it is a fetcher (many photos), then the first one will be returned.

{{ user.data.photos|photo:'normal' }} The url of the first photo in the list will be displayed, without the baseurl.

|count

Filter: count – Return the LIMITed count of a fetcher object. (This will be the number of rows returned taking into account LIMITs). This also works on arrays (where the number of items are returned) or any other data type (where 1 will be returned).

{{fetcher|count}} See {@link zajFetcher->count} for more details.

|total

Filter: total – Return the total number of object in this fetcher. (This will be the number of rows returned independent of any LIMIT clause or pagination)

{{fetcher|total}} See {@link zajFetcher->total} for more details.

|truncate

Filter: truncate – Truncates the variable to the number specified by parameter.

{{variable|truncate:'5'}} Truncates the length of variable string to 5 characters. So ‘Superdooper’ will be ‘Super…’

|escape

Filter: escape – Convert strings in various ways to escape certain “evil” characters. Keep in mind that Outlast Framework already has built-in XSS filtering, but be aware that this may not necessarily encode/decode the characters you want.

{{variable|escape}} Will convert &, ‘, “, <, and > to their HTML equivalents.
{{variable|escape:'url'}} Variable will be converted to a url-encoded string.

Escape takes a number of different values as its parameter:
htmlentities, htmlall: convert all applicable characters to HTML codes.
decode: convert all html entities to characters. this is html_entity_decode().
url: encode url entities (such as ? or &)
quotes, javascript, js: escape new lines and quotes with
mail: convert the string to a user[at]domain[dot]com for some minimal spam protection
htmlquotes: convert ‘ and ” to their HTML equivalents
htmlspecialchars, html (default): convert &, ‘, “, <, and > to their HTML equivalents
shellarg: same as running escapeshellarg() in php on the variable.
shellcmd: same as running escapeshellcmd() in php on the variable.

|escapejs

Filter: escapejs – Shorthand for |escape:’js’

See |escape for details.

|paginate

Filter: paginate – Paginates the fetcher object with the number per page set by the argument. By default, 10 per page.

{{fetcher|paginate:'50'}} Will list 50 items on this page. See {@link zajFetcher->paginate()} for more details.

|pagination

Filter: pagination – Displays the pagination widget.

{{fetcher|pagination:'apps'}} This will display the pagination widget with “apps” as the noun. See screenshot.

|sort

Filter: sort – Same as {@link zajlib_filter_base->filter_dictsort()

|rsort

Filter: rsort – Same as {@link zajlib_filter_base->filter_dictsortreversed()

|print_r

Filter: print_r – Returns the value of PHP’s print_r() function. Useful for debugging.

{{variable|print_r}} This is like running print_r(variable); in php.

|round

Filter: round – Round to the number of decimals specified by parameter (2 by default).

{{variable|round:'2'}} Assuming variable is 3.12355, the returned value will be 3.12.

|remainder

Filter: remainder – The variable is divided by the filter paramter and the remainder is returned.

{{variable|remainder:'3'}} Assuming variable is 8, the returned value will be 2.

|add

Filter: add – Add the amount specified by parameter from the variable. If either the variable or the parameter are non-numerical values, the two variables will be concatenated.

{{variable|subtract:1}} Assuming variable is 3, the returned value will be 2.
{{variable|subtract:' is a test.'}} Assuming variable is ‘This’, the returned value will be ‘This is a test.’.

|subtract

Filter: subtract – Subtract the amount specified by parameter from the variable.

{{variable|subtract:'1'}} Assuming variable is 3, the returned value will be 2.

|multiply

Filter: multiply – Multiply by the amount specified by parameter from the variable.

{{variable|multiply:'2'}} Assuming variable is 2, the returned value will be 4.

|divide

Filter: divide – Divide by the amount specified by parameter from the variable.

{{variable|divide:'2'}} Assuming variable is 4, the returned value will be 2.

|trim

Filter: trim – Trims characters (space by default) from left and right side of string.

{{' this has whitespace '|trim}} Will return ‘this has whitespace’.
{{'/url/with/trailing/slash/'|trim:'/'}} Will return without trailing or preceding slash, ‘url/with/trailing/slash’

|rtrim

Filter: rtrim – Same as |trim but trims characters (space by default) from right side of string.

|ltrim

Filter: ltrim – Same as |trim but trims characters (space by default) from left side of string.

|toquerystring

Filter: toquerystring – Converts an array to a query string.

{{variable|toquerystring:'name'}} Assuming variable is an array [‘red’, ‘white’, ‘blue’], the returned value will be name[0]=red&name[1]=white&name[2]=blue&.

|json_encode

Filter: json_encode – Converts a variable or object to its JSON value.

{{variable|json_encode}} Assuming variable is an array [‘red’, ‘white’, ‘blue’], the returned value will be its json equivalent.

|json_decode

Filter: json_decode – Decodes a JSON-encoded value.

{{variable|json_decode}} Assuming variable is an json-encoded string containing [‘red’, ‘white’, ‘blue’], the returned value will be the actual array.

|serialize

Filter: serialize – Converts a variable or object to its PHP-serialized value.

{{variable|serialize}} Assuming variable is an array [‘red’, ‘white’, ‘blue’], the returned value will be its PHP-serialized value. This is the same as using serialize() function in native PHP.

|unserialize

Filter: unserialize – Unserializes a PHP-serialized value.

{{variable|unserialize}} Assuming variable is a serialized string it will unserialize the value and return the actual native PHP data.

|srcset

Filter: srcset – Returns the HTML5 compatible srcset attribute value of an image. Can use a single Photo object or a list of Photo objects in which case the first will be used.

{{variable.data.photos|srcset}} The srcset=”[value comes here]” of the will be displayed for the first Photo in the Photos.
{{photo|srcset}} The srcset=”[value comes here]” of the will be displayed for this Photo.

|safe

Filter: safe – Disable automatic cross site scripting (XSS) checks on the variable. By default all variables are checked for XSS. This allows you to display <script> tags within the content. You should only use this on content where you are sure the source is safe, so typically no user input or perhaps trusted admin user input.

{{variable|safe}} This will disable the XSS checks on the variable.

|substr

Filter: substr – Cuts a string at the given value. See also truncate.

{{variable|substr:'5'}} Truncates the length of variable string to 5 characters. So ‘Superdooper’ will be ‘Super’

|querymode

Filter: querymode – Adds a ? or & to the end of the URL…whichever is needed.

{{url|querymode}} Assuming url is http://www.example.com/?q=1, it will return http://www.example.com/?q=1& and assuming URL is http://www.example.com/ it will return http://www.example.com/?

{{url|querymode:'some=more¶meters=gohere'}} Assuming url is http://www.example.com/?q=1, it will return http://www.example.com/?q=1&some=more&parameters=gohere and assuming URL is http://www.example.com/ it will return http://www.example.com/?some=more&parameters=gohere

|strftime

Filter: strftime – Format a local time/date according to locale settings

{{user.data.time_create|strftime:'%V,%G,%Y'}} = 1/3/2005
format – Uses the format of PHP’s strftime function.

|printf

Filter: printf – Allows substitutions in a string value. This is especially useful for localization.

{{#translated_string#|printf:'16'}} Assuming translated_string is ‘There are %1 registered’ it will return ‘There are 16 registered users’. Of course, ’16’ can be replaced with a variable as such: {{#translated_string#|printf:users.total}}. You can also chain these filters and use %2, %3, etc as the variables.

|translate

Filter: translate – Translate a string to another locale.

You must use a translation field as the input such as {{product.translation.name|translate:’hu_HU’}}. A warning will be generated if you try to use product.data.name instead.

{{product.translation.name|translate:'sk_SK'}} Will return the localized version of the product name. Without the filter, the current locale’s value is shown.

|explode

Filter: explode – Opposite of join, explodes a string by a character. Same as PHP’s explode().

{{'comma,separated,value'|explode:','}} Returns a list separated by comma.

|keyvalue

Filter: keyvalue – Returns value of an item in a list, object, or array.

Useful if you want to fetch a value by its key.

{{list|keyvalue:itemkey}} If itemkey is ‘somekey’ then this will return {{list.somekey}}.

Here are some useful examples:

|is_dict

Filter: is_dict – Returns true if dictionary/list. Dictionaries/lists are arrays and/or objects.

{{myvar|is_dict}} Returns true if myvar is an array or object.

|in

Filter: in – You can check if an item is contained within another. This is especially useful for lists.

If you check in a list, it will look for an object within that list. If you check in a string, it will check if the string is in the other. This is very similar to django’s in operator {@link https://docs.djangoproject.com/en/1.2/ref/templates/builtins/#in-operator}

{{product|in:topproducts}} Will return true if the object product is found within the list topproducts. Also works for arrays.
{{'something'|in:'A sentence about something.'}} Will return true if the string is located within the string.

|gravatar

Filter: gravatar – Converts an email to a gravatar image url.

See the php implementation of Gravatar image generation.

{{user.data.email|gravatar}} Returns the full url of the user’s Gravatar image. The width will be 50 by default.
{{user.data.email|gravatar:100}} Returns the full url of the user’s Gravatar image with a pixel width size of 100.

|strip_attribute

Filter: strip_attribute – Removes a given attribute from any html tags in the variable. This is not intended as a security feature, it is merely a helper filter.

{{ product.data.description|strip_attribute:'style' }} Will remove in-line styling from html tags.

Outlast Web & Mobile Development (c) 2023 | Privacy Policy |