On Dec 15, 2006, at 12:09 PM, Christopher Arndt wrote:

>
> [EMAIL PROTECTED] wrote:
>> I have a group of widgets that I would like to pass to all  
>> templates in
>> my site.  Do I have to include them in the return value of each of my
>> controllers (possibly using a decorator)?  Or is there an easier way?
>
> Instantiate the widgets somewhere in your code and then include  
> them in
> the tg.include_widgets config variable, like so:
>
> tg.include_widgets = [
>    'turbogears.mochikit',
>    'myproject.sitewidgets.mywidget',
>    'myproject.sitewidgets.myotherwidget'
> ]
>
>
> You can do this in your code without the need to touch the config  
> files:
>
> from turbogears import config
>
> def register_widgets(widgets, pkg_name):
>      """Include site-wide widgets on every page.
>
>      'widgets' is a list of widget instance names.
>
>      Order of the widget names is important for proper inclusion
>      of JavaScript and CSS.
>
>      The named widget instances have to be instantiated in
>      some of your modules.
>
>      'pkg_name' is the name of your Python module/package, in which
>      the widget instances are defined.
>      """
>
>      # first get widgets listed in the config files
>      include_widgets = config.get('tg.include_widgets', [])
>      # then append given list of widgets
>      for widget in widgets:
>          include_widgets.append('%s.%s' % (pkg_name, widget))
>      config.update({'global': {'tg.include_widgets':  
> include_widgets}})
>
> _sitewidgets = [
>    'mywidget',
>    'myotherwidget'
> ]
>
> register_widgets(_sitewidgets, 'myproject.sitewidgets')
>
>
>> I tried importing and referencing them direct from my templates, but
>> javascript etc doesn't work :(
>
> Yes, the widgets must be either in the dict retrned from your
> controlller method or inluded in tg.include_widgets for the automatic
> inclusion of JS/CSS to work.


And to display them from your template reference them as "tg_%s" %  
widget_instance_name. For example:

"myapp.this_widget" will be "tg_this_widget" in the template.

Alberto

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to