Pass variables to admin template

26 views
Skip to first unread message

Mikhail

unread,
Feb 28, 2020, 12:13:44 PM2/28/20
to TurboGears
Hello!

I have master.xhtml with some kind of menu:
<ul class="menu">
   
<li py:for="pg in pages">
       
<a href="/pages/${pg.name}" py:content="pg.title">Page</a>
   
</li>

</ul>

Now when I am using AdminController I get error "builtins.NameError". NameError: name 'pages' is not defined

Question: Is there a way to pass pages to AdminController's output?

As workaround I tried to use custom layout without menu:
class CustomAdminConfig(TGAdminConfig):
    default_index_template
= 'admin.xhtml'

class RootController(BaseController):
    admin
= AdminController(model, DBSession, config_type=CustomAdminConfig)

But it seems that admin.xhtml only applied to /admin page, not any of subpages such as /admin/users. And I prefer to have menu on all pages, including admin.

Alessandro Molina

unread,
Mar 7, 2020, 9:07:03 AM3/7/20
to TurboGears
There are few ways you can pass variables to all templates independently from what the controller is providing.
The most simple one is to use tg.tmpl_context which is meant to be a container for variables that should be shared by all templates, as tmpl_context is always available to all templates.
Otherwise you can set a variable_provider option in app_cfg, which can be set to a function (must return a dict) that will be executed when rendering the template to provide additional variables. 

In regard of editing the pages content themselves. The most simple solutions are to add the section in master.xhtml with a py:if that makes it visibile only when in the admin, or to use template patches ( https://github.com/TurboGears/tgext.pluggable#patching-templates ) to patch the templates with extra sections.
Otherwise Admin obviously support changing the expositions of the controllers and replacing the templates, but that will require providing new templates instead of just patching the existing ones.

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/turbogears/5a5db6e0-ab34-4a4d-8d32-82f622c5db7c%40googlegroups.com.

Mikhail

unread,
Mar 26, 2020, 12:50:47 PM3/26/20
to TurboGears
Thank you!

Looks like tmpl_context is what I want. Just one more thing: Where to put it to be accessible from admin template? Here is my code:

class RootController(BaseController):
    tmpl_context.pages = getPages()

    admin = AdminController(model, DBSession, config_type=CustomAdminConfig)

If I put it like this tmpl_context.pages is avaliable only on first page load after engine restart.
This:

    admin.tmpl_context.pages = getPages()

gives me AttributeError: 'AdminController' object has no attribute 'tmpl_context'


суббота, 7 марта 2020 г., 17:07:03 UTC+3 пользователь Alessandro Molina написал:
There are few ways you can pass variables to all templates independently from what the controller is providing.
The most simple one is to use tg.tmpl_context which is meant to be a container for variables that should be shared by all templates, as tmpl_context is always available to all templates.
Otherwise you can set a variable_provider option in app_cfg, which can be set to a function (must return a dict) that will be executed when rendering the template to provide additional variables. 

In regard of editing the pages content themselves. The most simple solutions are to add the section in master.xhtml with a py:if that makes it visibile only when in the admin, or to use template patches ( https://github.com/TurboGears/tgext.pluggable#patching-templates ) to patch the templates with extra sections.
Otherwise Admin obviously support changing the expositions of the controllers and replacing the templates, but that will require providing new templates instead of just patching the existing ones.

On Fri, Feb 28, 2020 at 6:13 PM Mikhail <pus...@gmail.com> wrote:
Hello!

I have master.xhtml with some kind of menu:
<ul class="menu">
   
<li py:for="pg in pages">
       
<a href="/pages/${pg.name}" py:content="pg.title">Page</a>
   
</li>

</ul>

Now when I am using AdminController I get error "builtins.NameError". NameError: name 'pages' is not defined

Question: Is there a way to pass pages to AdminController's output?

As workaround I tried to use custom layout without menu:
class CustomAdminConfig(TGAdminConfig):
    default_index_template
= 'admin.xhtml'

class RootController(BaseController):
    admin
= AdminController(model, DBSession, config_type=CustomAdminConfig)

But it seems that admin.xhtml only applied to /admin page, not any of subpages such as /admin/users. And I prefer to have menu on all pages, including admin.

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbo...@googlegroups.com.

S.C. Justice

unread,
Mar 26, 2020, 7:40:16 PM3/26/20
to turbo...@googlegroups.com
if you look in the auto generated root controller you'll see an example of using tmpl_context. On  your controller make a _before method which will set tmpl_context.pages = getPages()

To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/turbogears/06fdbafd-10af-454f-addc-cabb78d81b62%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages