On Mon, 2011-03-07 at 17:24 -0800, Wyatt Baldwin wrote: > On Monday, March 7, 2011 2:56:18 PM UTC-8, Ben Bangert wrote: > On Mar 7, 2011, at 2:27 PM, Jens Hoffrichter wrote: > > > Luckily, I knew that from my more recent Pylons experiences, > and I knew I had to use webhelpers.html.literal() for that. > But how to get the webhelpers into the template (I was using > the default Chameleon templates here)? In the normal Pylons > Mako templates I have always h and c available, where c isn't > necessary in pyramid anymore, as I have the returned values > from the view there. After quite a bit of digging around in > the pyramid (which has 0 on that topic), and chameleon docs > (which has mostly BNF as a documentation, as far as I saw), I > found out that I could use something like this inside the > template: > > > > <div id="main" tal:define="h import:choosecourse.helpers"> > > > > I added a helpers.py, where I imported all my usual > webhelpers into it, and then had literal available in the > template. > > There is a cookbook entry for this: > > http://docs.pylonsproject.org/projects/pyramid_cookbook/dev/templates.html > > > Chris M. linked to that page, too, so I'm wondering if that's the > preferred way to add `h` to templates' global namespace. I did it like > this, which seems a little cleaner to me: > > # myapp/__init__.py > from myapp import helpers > > def main(global_config, **settings): > # ... > def renderer_globals_factory(system): > return dict( > h=helpers, > # ... > ) > config = Configurator( > settings=settings, > renderer_globals_factory=renderer_globals_factory, > ) > > Pretty much the same, but no need to think about events. > > Ref: > http://docs.pylonsproject.org/projects/pyramid/1.0/narr/hooks.html#adding-renderer-globals
Either way is fine. The mechanism you've used is of an earlier vintage and still works. The way you've done it offers more control; the BeforeRender event subscriber mechanism has some safeguards to protect against duplicate key additions, the way you do it above does not. Both are documented here: http://docs.pylonsproject.org/projects/pyramid/1.0/narr/hooks.html#adding-renderer-globals http://docs.pylonsproject.org/projects/pyramid/1.0/narr/hooks.html#using-the-before-render-event - C > > > -- > You received this message because you are subscribed to the Google > Groups "pylons-discuss" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to pylons-discuss > [email protected]. > For more options, visit this group at > http://groups.google.com/group/pylons-discuss?hl=en. -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
