Value Coercion

From NDjango
Jump to: navigation, search

When a tag or a filter needs some data, such data are expected to be of a certain data type. In many cases even though the data are not of the expected data type an attempt will be made to coerce the supplied data to a proper data type. Below you can find specific rules of such conversions.

Tags

{%for a,b,c in list%}

  • the list is a filter expression. The value of the expression as evaluated at runtime must implement IEnumerable. If it does not the list is considered to be empty.


{%extends name%}

  • name is an expression returning a string. No data type corecion is performed on the value


{%if expression %}

  • expression consists of filter expressions combined with logical operators. A filter expression is considered to be false, if
    • the expression type is boolean and its value is false
    • the expression type is IEnumerable and the collection is empty
    • the expression value is null
  • otherwise the value of the filter expression is cosidered to be true


{%ifequal exp1 exp2%}

  • filter expressions exp1 and exp2 are evaluated. If evaluation of both succeeds and values are the same the condition is considered to be satisfied.


{%ifnotequal exp1 exp2%}

  • filter expressions exp1 and exp2 are evaluated. If evaluation of both succeeds and values are different the condition is considered to be satisfied.


{%include name%}

  • name is an expression returning a string. No data type corecion is performed on the value


{%regroup source by grouper as result%}

  • source is a filter expression to return a collection. If the value of the evaluated expression does not implement IEnumerable the tag returns an empty list

Filters

The table below lists all data coercion rules used by filters. If the data supplied to the filter can not be converted to the necessary data type the runtime will throw an exception. Depending on the context of the reference this exception can be suppressed. For example the following variable expression like {{var|add:"two"}} will cause an exception which will be suppressed and the expression will be replaced with the TEMPLATE_STRING_IF_INVALID string.

Note: for integer conversion algorithm - see Key_differences


Filter Name Variable Type Coercion Applied Parameter Type Coercion Applied
add Any converted to integer using conversion algorithm Any converted to integer using conversion algorithm
addslashes Any converted to string using Convert.ToString N/A
capfirst Any converted to string using Convert.ToString N/A
center Any converted to string using Convert.ToString Any converted to integer using conversion algorithm
cut Any converted to string using Convert.ToString N/A
date DateTime None Any Converted to string using Convert.ToString
Any other type Converted to string using Convert.ToString, then parsed using DateTime.TryParse
default Any converted to string using Convert.ToString N/A
default_if_none N/A N/A
dictsort Any type implementing IEnumerable containing IDictionary<string,IComparable> items No Conversion applied Any converted to string using Convert.ToString
dictsortreversed Any type implementing IEnumerable containing IDictionary<string,IComparable> items No Conversion applied Any converted to string using Convert.ToString
divisibleby Any converted to integer using conversion algorithm Any converted to integer using conversion algorithm
escapejs Any converted to string using Convert.ToString N/A
first Any type implementing IEnumerable None N/A
Any other type converted to string using Convert.ToString, then cast to IEnumerable
fix_ampersands Any converted to string using Convert.ToString N/A
floatformat Any converted first to string using Convert.ToString, then parsed using Decimal.TryParse with NumberStyles.Any Any converted to integer using conversion algorithm
force_escape Any converted to string using Convert.ToString N/A
get_digit Any converted to integer using conversion algorithm Any converted to integer using conversion algorithm
join Any type implementing IEnumerable items converted to string using Convert.ToString Any converted to string using Convert.ToString
Any other Throw exception
last Any type implementing IEnumerable None N/A
Any other type converted to string using Convert.ToString, then cast to IEnumerable
length Any type implementing IEnumerable None N/A
Any other type converted to string using Convert.ToString, then cast to IEnumerable
length_is Any type implementing IEnumerable None Any converted to integer using conversion algorithm
Any other type converted to string using Convert.ToString, then cast to IEnumerable
linebreaks/linebreaksbr Any converted to string using Convert.ToString N/A
linenumbers Any converted to string using Convert.ToString N/A
ljust Any converted to string using Convert.ToString Any converted to integer using conversion algorithm
lower Any converted to string using Convert.ToString N/A
makelist Any type implementing IEnumerable None N/A
Any other type converted to string using Convert.ToString, then cast to IEnumerable
random Any type implementing IEnumerable None N/A
Any other type converted to string using Convert.ToString, then cast to IEnumerable
removetags Any converted to string using Convert.ToString N/A
rjust Any converted to string using Convert.ToString Any converted to integer using conversion algorithm
slice Any type implementing IEnumerable None Any converted to string using Convert.ToString
Any other type converted to string using Convert.ToString, then cast to IEnumerable
slugify Any converted to string using Convert.ToString N/A
striptags Any converted to string using Convert.ToString N/A
time DateTime None Any Converted to string using Convert.ToString
Any other type Converted to string using Convert.ToString, then parsed using DateTime.TryParse
timesince/timeuntil DateTime None DateTime None
Any other type Converted to string using Convert.ToString, then parsed using DateTime.TryParse Any other type Converted to string using Convert.ToString, then parsed using DateTime.TryParse
title Any converted to string using Convert.ToString N/A
truncatewords Any converted to string using Convert.ToString Any converted to integer using conversion algorithm
upper Any converted to string using Convert.ToString N/A
ulencode Any converted to string using Convert.ToString N/A
urlize Any converted to string using Convert.ToString N/A
urlizetrunc Any converted to string using Convert.ToString Any converted to integer using conversion algorithm
wordcount Any converted to string using Convert.ToString N/A
wordwrap Any converted to string using Convert.ToString Any converted to integer using conversion algorithm