2

I've been trying hard to understand this, but can't quite put a finger on a precise documentation about it. I am quite confused about the different meaning of context in this Python Pyramid+Mako setup.

Here is some code snippets (tell me if you need more context):

class Root(object):
    request = None
    def __init__(self, request):
        self.request = request

@events.subscriber(events.BeforeRender)
def add_renderer_globals(event):
    event[u'c'] = event[u'request'].tmpl_context
    print u"add_renderer_globals(): request.tmpl_context={0}".format(event[u'request'].tmpl_context)
    print u"add_renderer_globals(): context={0}".format(event[u'context'])

@view.view_config(route_name='login', request_method='GET', renderer='login.mako')
def login_get(context, request):
    print u"login_get(): context={0}".format(context)
    return {}

[...]
cfg = config.Configurator(root_factory=Root,
        package=MyPKG,
        settings=settings,
        session_factory=pyramid_beaker.session_factory_from_settings(settings),
        )

cfg.add_route(name='login', pattern='/login')

cfg.scan()

and in my mako template, just to have an example, I only have:

Mako template context=${context}

So I would make a request and I get the following outputs from console or browser:

login_get(): context=<MyPKG.Root object at 0x1523c90>
add_renderer_globals(): request.tmpl_context=<pyramid.request.TemplateContext object at 0x12fbc50>
add_renderer_globals(): context=<MyPKG.Root object at 0x1523c90>
Mako template context=<mako.runtime.Context object at 0x15a4950>

My question is: What are the differences, and what do you use them for? I'm also confused why semantically, I declared root_factory=MyPKG.Root and it becomes context=MyPKG.Root in my view and my subscriber.

Thanks for any hint to help me understand.

Danosaure
  • 3,578
  • 4
  • 26
  • 41

1 Answers1

4

First, ignore request.tmpl_context. This is just a dictionary on the request object that you can add stuff to and is not normally used in Pyramid applications at all. It's a step-child from the Pylons merge.

There are two context objects when using Mako. The first (mako.runtime.Context) is supplied by Mako: http://docs.makotemplates.org/en/latest/runtime.html#context

Pyramid typically exposes the traversal context (MyPKG.Root) as context in your templates. However, Mako already has a variable using that name. :-( Thus, Pyramid's context is actually named _context.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70
  • I believe noone else will think of a better explanation, so I'll accept this answer as it answers my questions. – Danosaure Feb 22 '12 at 04:21
  • @michael-merickel: Can we find this info in pyramid doc? If not, should we update it? Thx! – Hadrien Apr 11 '12 at 23:01
  • afaik the `_context` is undocumented.. https://github.com/Pylons/pyramid/issues/530, however `request.tmpl_context` serves very little purpose and is actually docs-deprecated now. – Michael Merickel Apr 12 '12 at 02:38