Ticket #7153: 7153.diff

File 7153.diff, 3.3 KB (added by Alexander Koshelev, 15 years ago)

Better path with tests for current trunk

  • django/template/__init__.py

     
    703703            except (TypeError, AttributeError, KeyError):
    704704                try: # attribute lookup
    705705                    current = getattr(current, bit)
    706                     if callable(current):
    707                         if getattr(current, 'alters_data', False):
    708                             current = settings.TEMPLATE_STRING_IF_INVALID
    709                         else:
    710                             try: # method call (assuming no args required)
    711                                 current = current()
    712                             except TypeError: # arguments *were* required
    713                                 # GOTCHA: This will also catch any TypeError
    714                                 # raised in the function itself.
    715                                 current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call
    716                             except Exception, e:
    717                                 if getattr(e, 'silent_variable_failure', False):
    718                                     current = settings.TEMPLATE_STRING_IF_INVALID
    719                                 else:
    720                                     raise
    721706                except (TypeError, AttributeError):
    722707                    try: # list-index lookup
    723708                        current = current[int(bit)]
     
    732717                        current = settings.TEMPLATE_STRING_IF_INVALID
    733718                    else:
    734719                        raise
     720            if callable(current):
     721                if getattr(current, 'alters_data', False):
     722                    current = settings.TEMPLATE_STRING_IF_INVALID
     723                else:
     724                    try: # method call (assuming no args required)
     725                        current = current()
     726                    except TypeError: # arguments *were* required
     727                        # GOTCHA: This will also catch any TypeError
     728                        # raised in the function itself.
     729                        current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call
     730                    except Exception, e:
     731                        if getattr(e, 'silent_variable_failure', False):
     732                            current = settings.TEMPLATE_STRING_IF_INVALID
     733                        else:
     734                            raise
    735735
    736736        return current
    737737
  • tests/regressiontests/templates/tests.py

     
    299299            'basic-syntax25': ('{{ "fred" }}', {}, "fred"),
    300300            'basic-syntax26': (r'{{ "\"fred\"" }}', {}, "\"fred\""),
    301301            'basic-syntax27': (r'{{ _("\"fred\"") }}', {}, "\"fred\""),
     302           
     303            # resolve function in context top level
     304            'basic-syntax28': (r'{{foobar}}', {"foobar": lambda: "foo bar"}, "foo bar"),
    302305
    303306            # List-index syntax allows a template to access a certain item of a subscriptable object.
    304307            'list-index01': ("{{ var.1 }}", {"var": ["first item", "second item"]}, "second item"),
Back to Top