Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZT should allow dictionary access if attribute access fails on an object #8

Open
GoogleCodeExporter opened this issue Aug 22, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

Django supports accessing dictionary entries when attribute accesses fail, 
which is a really handy feature in my experience. It'd be nice if EZT supported 
this as well.

What steps will reproduce the problem?

1. Reference an entry in a dictionary in your template somewhere,
   e.g. <h1>[mydict.one]</h1>
2. Pass in a dictionary for the corresponding data variable to generate, 
   e.g. generate(fd, {'mydict': {'one': 1, 'two': 2}})

What is the expected output? What do you see instead?

In Django, the attribute access would fail, but the system would check that the 
object is a dictionary, and try a dictionary access, which would then return 1. 
In EZT, this simply fails with an UnknownReference exception

What version of the product are you using? On what operating system?

I'm not entirely sure what version I'm using, but the _get_value function looks 
no different when compared with the trunk version in this repository.

Please provide any additional information below.

Something like this seems to work for me:

@@ -672,7 +672,11 @@ def _get_value((refname, start, rest), ctx, filename, 
line_number):
     try:
       ob = getattr(ob, attr)
     except AttributeError:
-      raise UnknownReference(refname, filename, line_number)
+      if (hasattr(ob, 'has_key') and hasattr(ob, '__getitem__')
+          and ob.has_key(attr)):
+        ob = ob[attr]
+      else:
+        raise UnknownReference(refname, filename, line_number)

   # make sure we return a string instead of some various Python types
   if isinstance(ob, (IntType, FloatType, LongType)):

Original issue reported on code.google.com by [email protected] on 30 Aug 2010 at 12:42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants