<s> """<STR_LIT>""" <EOL> from django import template <EOL> register = template . Library ( ) <EOL> @ register . tag <EOL> def switch ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> args = token . split_contents ( ) <EOL> if len ( args ) != <NUM_LIT:2> : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % args [ <NUM_LIT:0> ] ) <EOL> childnodes = parser . parse ( ( "<STR_LIT>" , ) ) <EOL> parser . delete_first_token ( ) <EOL> casenodes = childnodes . get_nodes_by_type ( CaseNode ) <EOL> return SwitchNode ( args [ <NUM_LIT:1> ] , casenodes ) <EOL> @ register . tag <EOL> def case ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> args = token . split_contents ( ) <EOL> assert len ( args ) == <NUM_LIT:2> <EOL> children = parser . parse ( ( "<STR_LIT>" , ) ) <EOL> parser . delete_first_token ( ) <EOL> return CaseNode ( args [ <NUM_LIT:1> ] , children ) <EOL> class SwitchNode ( template . Node ) : <EOL> def __init__ ( self , value , cases ) : <EOL> self . value = value <EOL> self . cases = cases <EOL> def render ( self , context ) : <EOL> try : <EOL> value = template . resolve_variable ( self . value , context ) <EOL> except VariableDoesNotExist : <EOL> return "<STR_LIT>" <EOL> for case in self . cases : <EOL> if case . equals ( value , context ) : <EOL> return case . render ( context ) <EOL> return "<STR_LIT>" <EOL> class CaseNode ( template . Node ) : <EOL> def __init__ ( self , value , childnodes ) : <EOL> self . value = value <EOL> self . childnodes = childnodes <EOL> def equals ( self , otherval , context ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return template . resolve_variable ( self . value , context ) == otherval <EOL> except VariableDoesNotExist : <EOL> return False <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return self . childnodes . render ( context ) </s>
