(parser, token)
return CaseNode(lookup_expr, branches, default)
Hash-lookup branching tag

    Branch on value expression with optional default on no match::

        {% case place.value "first" %}
            You won first place!
        {% case "second" "third" %}
            Great job!
        {% else %}
            Practice is the way to success.
        {% endcase %}

    Each case must have at least one value. Case values must be literal,
    not expressions, and case values must be unique. An error is raised
    if more than one case exists with the same value.
Hash-lookup branching tag
Hash - lookup branching tag
def case(parser, token):
    """Hash-lookup branching tag

    Branch on value expression with optional default on no match::

        {% case place.value "first" %}
            You won first place!
        {% case "second" "third" %}
            Great job!
        {% else %}
            Practice is the way to success.
        {% endcase %}

    Each case must have at least one value. Case values must be literal,
    not expressions, and case values must be unique. An error is raised
    if more than one case exists with the same value.
    """
    args = token.split_contents()
    tag = args[0]
    if len(args) < 3:
        raise template.TemplateSyntaxError(
            "initial '{}' tag requires at least two arguments: a lookup "
            "expression and at least one value for the first case".format(tag))
    lookup_expr = parser.compile_filter(args[1])
    keys = [parse_literal(key, parser, tag) for key in args[2:]]
    branches = {}
    default = None
    while True:
        nodelist = parser.parse(("case", "else", "endcase"))
        for key in keys:
            if key in branches:
                raise template.TemplateSyntaxError(
                    "duplicate case not allowed: {!r}".format(key))
            branches[key] = nodelist
        token = parser.next_token()
        args = token.split_contents()
        tag = args[0]
        if tag != "case":
            break
        if len(args) < 2:
            raise template.TemplateSyntaxError(
                "inner 'case' tag requires at least one argument")
        keys = [parse_literal(key, parser, tag) for key in args[1:]]
    if len(args) > 1:
        raise template.TemplateSyntaxError(
            "'{}' tag does not accept arguments".format(tag))
    if tag == "else":
        default = parser.parse(("endcase",))
        token = parser.next_token()
        args = token.split_contents()
        tag = args[0]
        if len(args) > 1:
            raise template.TemplateSyntaxError(
                "'{}' tag does not accept arguments, got {}".format(tag))
    assert tag == "endcase", token.contents
    parser.delete_first_token()
    return CaseNode(lookup_expr, branches, default)
def case ( parser , token ) : args = token . split_contents ( ) tag = args [ 0 ] if len ( args ) < 3 : raise template . TemplateSyntaxError ( "initial '{}' tag requires at least two arguments: a lookup " "expression and at least one value for the first case" . format ( tag ) ) lookup_expr = parser . compile_filter ( args [ 1 ] ) keys = [ parse_literal ( key , parser , tag ) for key in args [ 2 : ] ] branches = { } default = None while True : nodelist = parser . parse ( ( "case" , "else" , "endcase" ) ) for key in keys : if key in branches : raise template . TemplateSyntaxError ( "duplicate case not allowed: {!r}" . format ( key ) ) branches [ key ] = nodelist token = parser . next_token ( ) args = token . split_contents ( ) tag = args [ 0 ] if tag != "case" : break if len ( args ) < 2 : raise template . TemplateSyntaxError ( "inner 'case' tag requires at least one argument" ) keys = [ parse_literal ( key , parser , tag ) for key in args [ 1 : ] ] if len ( args ) > 1 : raise template . TemplateSyntaxError ( "'{}' tag does not accept arguments" . format ( tag ) ) if tag == "else" : default = parser . parse ( ( "endcase" , ) ) token = parser . next_token ( ) args = token . split_contents ( ) tag = args [ 0 ] if len ( args ) > 1 : raise template . TemplateSyntaxError ( "'{}' tag does not accept arguments, got {}" . format ( tag ) ) assert tag == "endcase" , token . contents parser . delete_first_token ( ) return CaseNode ( lookup_expr , branches , default )