ternary filter

pull/8792/head
Brian Coca 10 years ago
parent 984d551257
commit 88daac4971

@ -304,13 +304,17 @@ Get a random number from 1 to 100 but in steps of 10::
Other Useful Filters
--------------------
To use one value on true and another on false::
{{ name == "John" | ternary('Mr','Ms') }}
To concatenate a list into a string::
{{ list | join(" ") }}
To get the last name of a file path, like 'foo.txt' out of '/etc/asdf/foo.txt'::
{{ path | basename }}
{{ path | basename }}
To get the directory from a path::

@ -146,6 +146,13 @@ def regex_replace(value='', pattern='', replacement='', ignorecase=False):
_re = re.compile(pattern, flags=flags)
return _re.sub(replacement, value)
def ternary(value, true_val, false_val):
''' value ? true_val : false_val '''
if value:
return true_val
else:
return false_val
def unique(a):
if isinstance(a,collections.Hashable):
c = set(a)
@ -293,6 +300,9 @@ class FilterModule(object):
'regex': regex,
'regex_replace': regex_replace,
# ? : ;
'ternary': ternary,
# list
'unique' : unique,
'intersect': intersect,

Loading…
Cancel
Save