Django

Code

Ticket #3826: with_tag.patch

File with_tag.patch, 3.4 kB (added by SmileyChris, 2 years ago)

with tests and docs

  • django/template/defaulttags.py

    old new  
    354354            return '' 
    355355        return str(int(round(ratio))) 
    356356 
     357class WithNode(Node): 
     358    def __init__(self, var, name, nodelist): 
     359        self.var = var 
     360        self.name = name 
     361        self.nodelist = nodelist 
     362 
     363    def __repr__(self): 
     364        return "<WithNode>" 
     365 
     366    def render(self, context): 
     367        val = self.var.resolve(context) 
     368        context.push() 
     369        context[self.name] = val 
     370        output = self.nodelist.render(context) 
     371        context.pop() 
     372        return output 
     373 
    357374#@register.tag 
    358375def comment(parser, token): 
    359376    """ 
     
    967984    return WidthRatioNode(parser.compile_filter(this_value_expr), 
    968985                          parser.compile_filter(max_value_expr), max_width) 
    969986widthratio = register.tag(widthratio) 
     987 
     988#@register.tag 
     989def do_with(parser, token): 
     990    """ 
     991    Add a value to the context (inside of this block) for caching and easy 
     992    access. For example:: 
     993 
     994        {% with person.some_sql_method as total %} 
     995            {{ total }} object{{ total|pluralize }} 
     996        {% endwith %} 
     997    """ 
     998    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] 
     1003    nodelist = parser.parse(('endwith',)) 
     1004    parser.delete_first_token() 
     1005    return WithNode(var, name, nodelist) 
     1006do_with = register.tag('with', do_with) 
  • docs/templates.txt

    old new  
    878878above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5 
    879879which is rounded up to 88). 
    880880 
     881with 
     882~~~~ 
     883 
     884**New in Django development version** 
     885 
     886Useful for caching a method which will be used more than once. 
     887 
     888For example:: 
     889 
     890    {% with person.some_sql_method as total %} 
     891        {{ total }} person object{{ total|pluralize }} 
     892    {% endwith %} 
     893 
     894The populated variable (in the example above, ``total``) is only available 
     895inside of ``{% with %}`` block. 
     896 
    881897Built-in filter reference 
    882898------------------------- 
    883899 
  • tests/regressiontests/templates/tests.py

    old new  
    650650            'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError), 
    651651            'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError), 
    652652 
     653            ### WITH TAG ######################################################## 
     654            'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), 
     655            'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')), 
     656 
    653657            ### NOW TAG ######################################################## 
    654658            # Simple case 
    655659            'now01' : ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)),