def do_menu(parser, token): """ Determines the correct menu to show for the location. Syntax:: {% get_menu [MenuLocation] as [varname] %} Sample usage:: {% get_menu "MainNavigation" as main_nav %} <ul> {% for item in main_nav.items %} <li {% ifequal item main_nav.active_item %}class="active"{% endifequal %}> <a href="{{ item.active_url }}">{{ item.name }}</a> </li> {% enfor %} </ul> """ bits = split_contents(token) if len(bits) != 4: raise TemplateSyntaxError, "get_menu: invalid syntax" return GetMenuNode(bits[1], bits[3])
def do_render(parser, token): """ Loads a template and renders it with the current context plus any passed parameters. The template name can be a concatenation of values by using a plus-sign ('+') delimiter. Note that there cannot be spaces between the values and the plus sign(s). Examples:: {% render "foo/some_include.html" %} {% render "foo/some_include.html" foo=4 bar="hello world" zed=somevariable %} {% render somevar+".html" foo=4 %} {% render "test-"+somevar+".html" bar="hello world" %} """ bits = split_contents(token) params = {} if len(bits) > 1: param_bits = bits[2:] for param in param_bits: subbits = param.split('=') if len(subbits) != 2: raise TemplateSyntaxError, "render parameters must be in name=value format" params[subbits[0]] = subbits[1] return RenderNode(bits[1], params)
def do_default(parser, token): """ Sets a context variable to a default value if the variable is currently undefined. Examples:: {% set_default showPhoto True %} {% set_default width 250 %} {% set_default name "John Smith" %} """ bits = split_contents(token) if len(bits) != 3: raise TemplateSyntaxError, "set_default: Invalid template tag parameters." return DefaultNode(bits[1], bits[2])
def do_get(parser, token): # prepare our data structures bits = split_contents(token) params = {} varname = None keywords = ['with','from','in','order','limit','offset'] # remove the template tag name from the bits list bits.pop(0) # check that we have been provided with the desired variable name # before doing all the parsing if bits[-2] == "as": varname = bits.pop() bits.pop() else: raise TemplateSyntaxError, "get tag: must end tag with 'as <varname>'" # check that some parameters were passed if len(bits) < 2: raise TemplateSyntaxError, "get tag: the content type and at least one parameter are required" params['app_model'] = bits.pop(0) # form 2 if bits and bits[0] not in keywords: params['names'] = bits return GetByNameNode(params, varname) # with clause if bits and bits[0] == "with": bits.pop(0) if len(bits) >= 2 and bits[1] not in keywords: params['with_field'] = bits.pop(0) params['with_value'] = bits.pop(0) elif len(bits) >= 1: params['with_field'] = bits.pop(0) else: raise TemplateSyntaxError, "get tag: invalid syntax in 'with' clause; must be in form 'with <field> <value>' or 'with <m2m_field>'" # in clauses while bits and (bits[0] == "in" or (bits[0] == "not" and bits[1] == "in")): if 'in' not in params: params['in'] = [] if bits[0] == "not": exclude = True bits.pop(0) else: exclude = False bits.pop(0) if len(bits) >= 2: params['in'].append({ 'type': bits.pop(0), 'obj': bits.pop(0), 'exclude': exclude }) else: raise TemplateSyntaxError, "get tag: invalid syntax in 'in' clause; must be in form '(not) in <type> <object>'" # from last clause if bits and bits[0] == "from": bits.pop(0) if len(bits) >= 3 and (bits[0] == "last" or bits[0] == "next" or bits[0] == "week"): params['from_side'] = bits.pop(0) params['from_x'] = bits.pop(0) params['from_timeperiod'] = bits.pop(0) else: raise TemplateSyntaxError, "get tag: invalid syntax in 'from last' clause; must be in form 'from last <#> <timeperiod>'" # order by clause if bits and bits[0] == "order": bits.pop(0) if len(bits) >= 2 and bits[0] == "by": bits.pop(0) params['order_by'] = bits.pop(0) if params['order_by'] in keywords: raise TemplateSyntaxError, "get tag: invalid syntax in 'order by' clause. must be in form 'order by <field>'" else: raise TemplateSyntaxError, "get tag: invalid syntax in 'order by' clause; must be in form 'order by <field>'" # limit ... offset clause if bits and bits[0] == "limit": bits.pop(0) if len(bits) >= 1: params['limit'] = bits.pop(0) if params['limit'] in keywords: raise TemplateSyntaxError, "get tag: invalid syntax in 'limit' clause. must be in form 'limit <number>'" else: raise TemplateSyntaxError, "get tag: invalid syntax in 'limit' clause. must be in form 'limit <number>'" # optional offset clause if bits and bits[0] == "offset": bits.pop(0) if len(bits) >= 1: params['offset'] = bits.pop(0) if params['offset'] in keywords: raise TemplateSyntaxError, "get tag: invalid syntax in 'offset' clause. must be in form 'offset <number>'" else: raise TemplateSyntaxError, "get tag: invalid syntax in 'offset' clause. must be in form 'offset <number>'" if bits and bits[0] == "offset": raise TemplateSyntaxError, "get tag: 'offset' must follow a 'limit' value" return GetNode(params, varname)