Ticket #13567: defaulttags.patch

File defaulttags.patch, 2.3 KB (added by anentropic, 14 years ago)

backwards compatible with 'silent mode' flag

  • defaulttags.py

     
    5353            return u''
    5454
    5555class CycleNode(Node):
    56     def __init__(self, cyclevars, variable_name=None):
     56    def __init__(self, cyclevars, variable_name=None, silent=None):
    5757        self.cyclevars = cyclevars
    5858        self.variable_name = variable_name
     59        self.silent = silent
    5960
    6061    def render(self, context):
    6162        if self not in context.render_context:
     
    6465        value = cycle_iter.next().resolve(context)
    6566        if self.variable_name:
    6667            context[self.variable_name] = value
     68            if self.silent is not None and self.silent.resolve(context):
     69                return u''
    6770        return value
    6871
    6972class DebugNode(Node):
     
    475478    You can use any number of values, separated by spaces. Commas can also
    476479    be used to separate values; if a comma is used, the cycle values are
    477480    interpreted as literal strings.
     481   
     482    You can now also pass a 'silent' flag (a var which will be evaluated as
     483    boolean) to supress the return string when you are using the 'as' form, eg:
     484   
     485        {% cycle 'row1' 'row2' 'row3' as rowcolors 0 %} (no output)
     486   
    478487    """
    479488
    480489    # Note: This returns the exact same node on each {% cycle name %} call;
     
    505514        if not name in parser._namedCycleNodes:
    506515            raise TemplateSyntaxError("Named cycle '%s' does not exist" % name)
    507516        return parser._namedCycleNodes[name]
    508 
    509     if len(args) > 4 and args[-2] == 'as':
    510         name = args[-1]
    511         values = [parser.compile_filter(arg) for arg in args[1:-2]]
    512         node = CycleNode(values, name)
     517   
     518    try:
     519        as_index = args.index('as')
     520    except ValueError:
     521        as_index = None
     522   
     523    if len(args) > 4 and as_index is not None:
     524        name = args[as_index+1]
     525        values = [parser.compile_filter(arg) for arg in args[1:as_index]]
     526        if len(args) > as_index + 2:
     527            node = CycleNode(values, name, parser.compile_filter(args[as_index+2]))
     528        else:
     529            node = CycleNode(values, name)
    513530        if not hasattr(parser, '_namedCycleNodes'):
    514531            parser._namedCycleNodes = {}
    515532        parser._namedCycleNodes[name] = node
Back to Top