python - Need to make template tag from simple.tag -


I have followed simple template tags which gives list of values ​​what I need to rewrite it like template tags, so I return (cleaned_filter) variable

  can be used as @ register.simple_tag def show_active_filters list: filter_list = [] if cleaned_filter: If cleaned_filter [ 'checkbox'] key, Value in cleaned_filter ['checkbox'] (Items): The value value: filter_list.append ( '% s:% s of "% (key, value)) that' clean_filter [ 'to' '']: Filter_name, filter_values ​​cleaned_filter [ 'to_to ']. Items (): sorted_values ​​= sorted (filter_values.values ​​()) if len (sorted_values) & lt; 2: Try: filter_values ​​['from'] filter_str = u '% s: to% s'% (filter_name, sorted_values ​​[0]) except filter_list.append (filter_str): filter_str = u'% s :% (FILTER_NAME, sorted_values ​​[0]) filter_list.append (filter_str) and: filter_str = '% s: from% s - home% s'% (FILTER_NAME, sorted_values ​​[0], Sorted_values ​​[1]) filter_list.append (filter_str) Return filter_list  

The way I tried to do this is following

  @ register.tag (name = 'show_active_filters') def do_show_active_filters (parser, token): error = false try: TAG_NAME, cleaned_filter, _as, var_name = token.split_contents () as if _as = '': error = except the truth : Tru B = true, the error increased TemplatekTemplateSyntaxError, "invalid" stuff "left return CleanedFilterDictionary (cleaned_filter, Var_name) class CleanedFilterDictionary (template.Node): def __init __ (self, cleaned_filter, var_name = 'filter_vals'): render self.cleaned_filter = cleaned_filter self.var_name = var_name def (self-reference): reference [self.var_name] = get_active_filters (cleaned_filter) return '' def get_active_filters (cleaned_filter): filter_list = [] if cleaned_filter: If cleaned_filter [ ' Checkbox ']: [' checkbox '] for the value in the key, cleaned_filter Item () :. The price values: filter_list .append ( '% s:% s'% (key, value)) that cleaned_filter [ 'from_to']: for FILTER_NAME, cleaned_filter [ 'from_to'] in filter_values ​​items ( ):. Sorted_values ​​= sorted by (filter_values.values ​​()) if lane (sorted_value) & lt; 2: Try: filter_values ​​filter_str = u '% s: from% s' [' from ']% (FILTER_NAME, sorted_values ​​[0]) except filter_list.append (filter_str): filter_str = u'% s :% (FILTER_NAME, sorted_values ​​[0]) filter_list.append (filter_str) and: filter_str = '% s: from% s - home% s'% (FILTER_NAME, sorted_values ​​[0], Sorted_values ​​[1]) filter_list.append (filter_str) return filter_list  

class example

variable how can not access the cleaned_filter pass

< Div class = "text after" itemprop = "text". The rest of the should work as your original code

->

You can use the simple_tag decorator instead of just the code

Comments