Given a string mapping values for true, false, and (optionally) None, return one of those strings according to the value: ========== ====================== ================================== Value Argument Outputs ========== ====================== =====
(value, arg=None)
| 860 | |
| 861 | @register.filter(is_safe=False) |
| 862 | def yesno(value, arg=None): |
| 863 | """ |
| 864 | Given a string mapping values for true, false, and (optionally) None, |
| 865 | return one of those strings according to the value: |
| 866 | |
| 867 | ========== ====================== ================================== |
| 868 | Value Argument Outputs |
| 869 | ========== ====================== ================================== |
| 870 | ``True`` ``"yeah,no,maybe"`` ``yeah`` |
| 871 | ``False`` ``"yeah,no,maybe"`` ``no`` |
| 872 | ``None`` ``"yeah,no,maybe"`` ``maybe`` |
| 873 | ``None`` ``"yeah,no"`` ``"no"`` (converts None to False |
| 874 | if no mapping for None is given. |
| 875 | ========== ====================== ================================== |
| 876 | """ |
| 877 | if arg is None: |
| 878 | # Translators: Please do not add spaces around commas. |
| 879 | arg = gettext("yes,no,maybe") |
| 880 | bits = arg.split(",") |
| 881 | if len(bits) < 2: |
| 882 | return value # Invalid arg. |
| 883 | try: |
| 884 | yes, no, maybe = bits |
| 885 | except ValueError: |
| 886 | # Unpack list of wrong size (no "maybe" value provided). |
| 887 | yes, no, maybe = bits[0], bits[1], bits[1] |
| 888 | if value is None: |
| 889 | return maybe |
| 890 | if value: |
| 891 | return yes |
| 892 | return no |
| 893 | |
| 894 | |
| 895 | ################### |