Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#365 closed defect (fixed)

[patch] Change template.resolve_variable() to resolve hard-coded strings

Reported by: davidschein@… Owned by: Adrian Holovaty
Component: Template system Version:
Severity: minor Keywords: tag ifnotequal
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Change {% ifnotequal %} tag to accept hard-coded strings.
i.e.
{% ifnotequal app.name 'Core' %}
...
{% endifnotequal %}

The following patch basically just adds:
if path[0]==path[-1] and path[0] in ['"', "'"]: return path[1:-1]

but using a wrapping conditional instead of seperate returns.

Index: template.py

===================================================================

--- template.py	(revision 532)

+++ template.py	(working copy)

@@ -369,30 +369,33 @@

 
     (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
     """
-    current = context
-    bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
-    while bits:
-        try: # dictionary lookup
-            current = current[bits[0]]
-        except (TypeError, AttributeError, KeyError):
-            try: # attribute lookup
-                current = getattr(current, bits[0])
-                if callable(current):
-                    if getattr(current, 'alters_data', False):
-                        current = ''
-                    else:
-                        try: # method call (assuming no args required)
-                            current = current()
-                        except SilentVariableFailure:
+    if path[0]==path[-1] and path[0] in ['"', "'"]: #it's a hard-coded string
+        current = path[1:-1]
+    else:
+        current = context
+        bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
+        while bits:
+            try: # dictionary lookup
+                current = current[bits[0]]
+            except (TypeError, AttributeError, KeyError):
+                try: # attribute lookup
+                    current = getattr(current, bits[0])
+                    if callable(current):
+                        if getattr(current, 'alters_data', False):
                             current = ''
-                        except TypeError: # arguments *were* required
-                            current = '' # invalid method call
-            except (TypeError, AttributeError):
-                try: # list-index lookup
-                    current = current[int(bits[0])]
-                except (IndexError, ValueError, KeyError):
-                    raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute
-        del bits[0]
+                        else:
+                            try: # method call (assuming no args required)
+                                current = current()
+                            except SilentVariableFailure:
+                                current = ''
+                            except TypeError: # arguments *were* required
+                                current = '' # invalid method call
+                except (TypeError, AttributeError):
+                    try: # list-index lookup
+                        current = current[int(bits[0])]
+                    except (IndexError, ValueError, KeyError):
+                        raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute
+            del bits[0]
     return current
 
 def resolve_variable_with_filters(var_string, context):

Change History (3)

comment:1 by hugo <gb@…>, 19 years ago

Summary: Change {% ifnotequal %} tag to accept hard-coded strings[patch] Change {% ifnotequal %} tag to accept hard-coded strings

comment:2 by anonymous, 19 years ago

Summary: [patch] Change {% ifnotequal %} tag to accept hard-coded strings[patch] Change template.resolve_variable() to resolve hard-coded strings

comment:3 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

Fixed in [587].

Note: See TracTickets for help on using tickets.
Back to Top