I keep seeing the same question about the new default argument for the templating. This post will cover all possible ways to define default.
All the methods that were changed:
acosas_timestampasinatanatan2cosfloatlogroundsinsqrtstrptimetantimestamp_customtimestamp_localtimestamp_utc
Each of them have the exact same default functionality but a different number of arguments. There are multiple ways to use these functions, and this is where all the confusion lies.
Understanding arguments on filters and functions.
Letās take for example a fake function called my_fake_function. The documentation spells out the function as:
my_fake_function as a function
my_fake_function(value, mode="a", flag=False)
\ / \ / \ /
\ / \ / \ /
| \ / \ /
required arugment | \ /
| |
optional keyword argument optional keyword argument
my_fake_function as a filter
value | my_fake_function(mode="a", flag=False)
\ / \ / \ /
\ / \ / \ /
| \ / \ /
required arugment | \ /
| |
optional keyword argument optional keyword argument
From this documentation line, I know the following information:
-
my_fake_functionhas 3 arguments:value,mode, andflag. -
valueis a required argument. You can tell because it does not have a value set on it.modeandflagare optional arguments. This means you donāt need to provide the argument tomy_fake_functionin order for it to work. -
modeis an optional argument because it has a value attached to it already. You can also call outmodeby name when passing in the information. This is because it has a default value which makes it a keyword argument. If you donāt providemode,my_fake_functionwill use'a'asmode. -
flagis an optional argument because it has a value attached to it already. You can also call outflagby name when passing in the information. This is because it has a default value which makes it a keyword argument. If you donāt provideflag,my_fake_functionwill useFalseforflag.
Example 1
In this example we will use value of 4, mode of "a" and flag of True. Because mode is omitted, my_fake_function will use "a" because the documentation said so.
{{ my_fake_function(4, flag=True) }}
This is also valid, instead of naming flag=True, you can simply provide all 3 arguments.
{{ my_fake_function(4, "a", True) }}
You can also name all optional arguments.
{{ my_fake_function(4, mode="a", flag=True) }}
Example 2
In this example will use value of 7, mode of "b" and flag of False. Keep in mind that we can omit flag because itās False, but you can also provide it if you want. This gives us 4 options on how to write this out.
{{ my_fake_function(7, "b") }}
{{ my_fake_function(7, "b", False) }}
{{ my_fake_function(7, mode="b") }}
{{ my_fake_function(7, mode="b", flag=False) }}
Example 3
This will use value of 9, mode of ācā and flag of True. Because we are changing all 3 arguments, we have to provide all 3. Leaving us with the only 2 valid ways of writing it.
{{ my_fake_function(7, "c", True) }}
{{ my_fake_function(7, mode="c", flag=True) }}
Notes
- You cannot use the argument name when specifying a required argument. In this example, that means you cannot use
value=4when specifying the required arugmentvalue. - Not all functions are filters. The documentation will tell you what is a filter and what is a function.
- Not all filters are functions. The documentation will tell you what is a filter and what is a function.
Using them as a function.
2 arguments
breakdown
{{ function(value_for_argument1, value_for_argument2) }}
examples
# returns acos of 1, if fails returns 0
{{ acos(1, 0) }}
# returns as_timestamp of 1, if fails returns 0
{{ as_timestamp(1, 0) }}
# returns asin of 1, if fails returns 0
{{ asin(1, 0) }}
# returns atan of 1, if fails returns 0
{{ atan(1, 0) }}
# returns cos of 1, if fails returns 0
{{ cos(1, 0) }}
# returns 1.0, if fails returns 0
{{ float('1', 0) }}
# returns sin of 1, if fails returns 0
{{ sin(1, 0) }}
# returns sqrt of 1, if fails returns 0
{{ sqrt(1, 0) }}
# returns tan of 1, if fails returns 0
{{ tan(1, 0) }}
3 arguments
breakdown
{{ function(value_for_argument1, value_for_argument2, value_for_argument3) }}
examples
# returns time @ 10 am, if fails returns 0
{{ strptime("10:00", "%H:%M", 0) }}
# returns log of 1 with base 10, if fails returns 0
{{ log(1, 10, 0) }}
# returns atan2 of 1 at 2, if fails returns 0
{{ atan2(1, 2, 0) }}
# returns rounds 1.23 down precision of 0, if fails returns 0
{{ round(1.43, "floor", 0) }}
3 arguments but omitting the 2nd argument
breakdown
{{ function(value_for_argument1, argument3 = value_for_argument3) }}
examples
# returns log of 1 with base e because base (2nd argument) defaults to e, if fails returns 0
{{ log(1, default = 0) }}
# returns rounds 1.43 to even with a precision of 0
# because function (2nd argument) defaults to round-to-even, if fails returns 0
{{ round(1.43, default = 0) }}
Using them as filters
2 arguments
breakdown
{{ value_for_argument1 | function(value_for_argument2) }}
examples
# returns as_timestamp of 1, if fails returns 0
{{ 1 | as_timestamp(0) }}
# returns 1.0, if fails returns 0
{{ '1' | float(0) }}
# returns fully formatted time string 1 second past
# midnight January 1st 1970, if fails returns 0
{{ 1 | timestamp_local(0) }}
# returns fully formatted time string 1 second past
# midnight January 1st 1970, if fails returns 0
{{ 1 | timestamp_utc(0) }}
3 arguments
breakdown
{{ value_for_argument1 | function(value_for_argument2, value_for_argument3) }}
examples
# returns time @ 10 am, if fails returns 0
{{ "10:00" | strptime("%H:%M", 0) }}
# returns rounds 1.23 down precision of 0, if fails returns 0
{{ 1.43 | round("floor", 0) }}
3 arguments but omitting the 2nd argument
breakdown
{{ value_for_argument1 | function(argument3 = value_for_argument3) }}
examples
# returns rounds 1.23 down precision of 0, if fails returns 0
{{ 1.43 | round(default=0) }}
4 arguments
breakdown
{{ value_for_argument1 | function(value_for_argument2, value_for_argument3, value_for_argument4) }}
examples
# returns "00:00:01", which is 1 second past
# midnight January 1st 1970 in local time, if fails returns 0
{{ 1 | timestamp_custom("%H:%M:%S", True, 0) }}
4 arguments omitting the 3rd argument
breakdown
{{ value_for_argument1 | function(value_for_argument2, value_for_argument3, value_for_argument4) }}
examples
# returns "00:00:01", which is 1 second past
# midnight January 1st 1970 in local time (local_time defaults to True), if fails returns 0
{{ 1 | timestamp_custom("%H:%M:%S", default = 0) }}
Hopefully, you can see there are many ways to define default for all the functionand filters. Everything covered in this post is valid. Keep in mind that some functions are not filters and some filters are not functions. All of this is covered in the documentation. This post does not mix and match filters. Everything in the filter section is a filter, anything that is missing is not a filter. Same goes for the function section.
