title: Lazy variable lookup in Jinja templates
body: <p>I want to render a Jinja2 Template using a custom object implementing the <code>__getitem__</code> interface. The object implements a lazy variable look up because it is impossible to create a dictionary from it (the number of available variables is almost infinite, value retrieval works dynamically on the queried key).</p>
  
  <p>Is it possible to render a Jinja2 template using a context object?</p>
  
  <pre><code># Invalid code, but I'd like to have such an interface.
  #
  
  from jinja2 import Template
  
  class Context(object):
  
      def __getitem__(self, name):
          # Create a value dynamically based on `name`
          if name.startswith('customer'):
               key = name[len('customer_'):]
               return getattr(get_customer(), key)
          raise KeyError(name)
  
  t = Template('Dear {{ customer_first }},\n')
  t.render(Context())
  </code></pre>