0

Is there any analog of django context processors in turbogears2? In tg1 was stdvars, but not in tg2 anymore.

Explaining: I need to have some template tags, avaible on each page, without obvious declaring in each controller.

Mikhail
  • 809
  • 8
  • 16

2 Answers2

4

You have three possible solutions to achieve this.

First you can use tg.tmpl_context which is available inside every template as tmpl_context. You can fill the variables inside the BaseController.__call__ so that they are available everywhere.

Another approach is to register base_config.variable_provider inside app_cfg.py which must be a function that returns a dictionary of variables that will be available inside any template. Those variables will be overridden from the controller returned ones if there is a name collision, so it is a good way to provide defaults for controller returned variables.

Otherwise in recent versions it is also possible to register the before_render hook systemwide using base_config.register_hook inside app_cfg.py the callback can append and override any template parameter.

amol
  • 1,771
  • 1
  • 11
  • 15
1

I'm not sure if this would be the best way of doing it but you could add the following to app_globals.Global:

from genshi.core import Markup
self.foo = lambda: Markup("<div>my content here!!!</div>")

and then in your your templates:

${g.foo()}

or you could do it via an ajax request...

Nick Holden
  • 3,639
  • 3
  • 22
  • 12