Django

Code

Changeset 4830

Show
Ignore:
Timestamp:
03/27/07 12:25:56 (1 year ago)
Author:
jacob
Message:

Fixed #3826: added a {% with %}. Thanks, SmileyChris?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaulttags.py

    r4816 r4830  
    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): 
     
    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) 
  • django/trunk/docs/templates.txt

    r4804 r4830  
    873873which is rounded up to 88). 
    874874 
     875with 
     876~~~~ 
     877 
     878**New in Django development version** 
     879 
     880Useful for caching a method which will be used more than once. 
     881 
     882For example:: 
     883 
     884    {% with person.some_sql_method as total %} 
     885        {{ total }} person object{{ total|pluralize }} 
     886    {% endwith %} 
     887 
     888The populated variable (in the example above, ``total``) is only available 
     889inside of ``{% with %}`` block. 
     890 
    875891Built-in filter reference 
    876892------------------------- 
  • django/trunk/tests/regressiontests/templates/tests.py

    r4690 r4830  
    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