Django

Code

Ticket #3826: with_template_tag.patch

File with_template_tag.patch, 4.0 kB (added by michelts@gmail.com, 2 years ago)
  • trunk/django/template/defaulttags.py

    old new  
    355355        return str(int(round(ratio))) 
    356356 
    357357class WithNode(Node): 
    358     def __init__(self, var, name, nodelist): 
    359         self.var = var 
    360         self.name = name 
     358    def __init__(self, varlist, nodelist): 
     359        self.varlist = varlist 
    361360        self.nodelist = nodelist 
    362361 
    363362    def __repr__(self): 
    364363        return "<WithNode>" 
    365364 
    366365    def render(self, context): 
    367         val = self.var.resolve(context) 
    368         context.push() 
    369         context[self.name] = val 
     366        context.push() # start a new context 
     367        for var, name in self.varlist: 
     368            context[name] = var.resolve(context) 
    370369        output = self.nodelist.render(context) 
    371         context.pop() 
     370        context.pop() # delete the new context 
    372371        return output 
    373372 
    374373#@register.tag 
     
    988987#@register.tag 
    989988def do_with(parser, token): 
    990989    """ 
    991     Add a value to the context (inside of this block) for caching and easy 
     990    Add values to the context (inside of this block) for caching and easy 
    992991    access. For example:: 
    993992 
    994993        {% with person.some_sql_method as total %} 
    995994            {{ total }} object{{ total|pluralize }} 
    996995        {% endwith %} 
     996        {% with person.some_sql_method as total and person.name as name %} 
     997            {{ name }}: {{ total }} object{{ total| pluralize }} 
     998        {% endwith %} 
    997999    """ 
    9981000    bits = list(token.split_contents()) 
    999     if len(bits) != 4 or bits[2] != "as": 
    1000         raise TemplateSyntaxError, "%r expected format is 'value as name'" % tagname 
    1001     var = parser.compile_filter(bits[1]) 
    1002     name = bits[3] 
     1001    bits.pop(0) 
     1002    if not bits: 
     1003        raise TemplateSyntaxError, "'with' expected format is 'value as name' or 'value1 as name1 and value2 as name2...'" 
     1004    bitlist = [bitstr.split(' ') for bitstr in ' '.join(bits).split(' and ')] 
     1005    try: 
     1006        varlist = [(parser.compile_filter(var), name) for var, prep, name in bitlist] 
     1007    except ValueError: 
     1008        raise TemplateSyntaxError, "'with' expected format is 'value as name' or 'value1 as name1 and value2 as name2...'" 
    10031009    nodelist = parser.parse(('endwith',)) 
    10041010    parser.delete_first_token() 
    1005     return WithNode(var, name, nodelist) 
     1011    return WithNode(varlist, nodelist) 
    10061012do_with = register.tag('with', do_with) 
  • trunk/tests/regressiontests/templates/tests.py

    old new  
    653653            ### WITH TAG ######################################################## 
    654654            'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), 
    655655            'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')), 
     656            'with03': ('{{ key1 }}{{ key2 }}{% with dict.key1 as key1 and dict.key2 as key2 %}{{ key1 }}{{ key 2 }}-{{ dict.key1 }}{{ dict.key2 }}-{{ key1 }}{{ key2 }}{% endwith %}{{ key1 }}{{ key2 }}', {'dict': {'key1':50, 'key2':60}}, ('5060-5060-5060', 'INVALIDINVALID5060-5060-5060INVALIDINVALID')), 
    656657 
    657658            ### NOW TAG ######################################################## 
    658659            # Simple case 
  • trunk/docs/templates.txt

    old new  
    887887        {{ total }} employee{{ total|pluralize }} 
    888888    {% endwith %} 
    889889 
     890Or even:: 
     891 
     892    {% with business.name as name and business.employees.count as total %} 
     893        {{ name }}: employee{{ total|pluralize }} 
     894    {% endwith %} 
     895 
    890896The populated variable (in the example above, ``total``) is only available 
    891897between the ``{% with %}`` and ``{% endwith %}`` tags. 
    892898