TG2, changing template at runtime

7 views
Skip to first unread message

Sergei Beilin

unread,
Sep 7, 2009, 7:49:50 AM9/7/09
to TurboGears
While working on Pyramid CMS, I did not find a short way to change a
template right inside the controller method. The method is @expose()'d
with some default template, but there is a chance that (depending on
the object fetched from the database) we will want to change it (after
we fetch the object; for example, imagine that the template name is
stored in the database). How does one accomplish this (I remember
there was a siple way in TG1.x?)

Thanks in advance!

--
Sergei

alexbod...@gmail.com

unread,
Sep 7, 2009, 8:03:54 AM9/7/09
to turbo...@googlegroups.com

hi sergei,

i'm using the following code:

from tg import expose, validate, override_template

......

@expose()
def list(self, page_id, blob_type):
template = "genshi:tgext.pages.templates.admin.page_%s_list" % blob_type
override_template(self.list, template)
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups "TurboGears" group.
> To post to this group, send email to turbo...@googlegroups.com
> To unsubscribe from this group, send email to turbogears+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/turbogears?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
>



--
alex

Sergei Beilin

unread,
Sep 7, 2009, 8:09:52 AM9/7/09
to TurboGears
Hello Alex!

> from tg import expose, validate, override_template

Wow, override_template was *that* keyword (I was googling for
"changing template" or something like that)

Thanks!

p.s. Do You use XMPP/Jabber?

--
Sergei

alexbod...@gmail.com

unread,
Sep 7, 2009, 8:30:44 AM9/7/09
to turbo...@googlegroups.com

Sergei Beilin

unread,
Sep 7, 2009, 8:42:10 AM9/7/09
to TurboGears, alexbod...@gmail.com
Oops! This *does*not* work for the RestController? ;)
It just does not change the template (but works nice for a
BaseController for example)
:(

--
Sergei

Sergei Beilin

unread,
Sep 11, 2009, 2:16:14 AM9/11/09
to TurboGears
hehe! It was caused by `@without_trailing_slash` standing *before*
`@expose`: just changed the order of the decorators and it worked.

--
Sergei

Diez B. Roggisch

unread,
Sep 11, 2009, 3:25:43 AM9/11/09
to turbo...@googlegroups.com
Sergei Beilin schrieb:

> hehe! It was caused by `@without_trailing_slash` standing *before*
> `@expose`: just changed the order of the decorators and it worked.

This shouldn't make a differenc, can you file a bug?

Diez

Sergei Beilin

unread,
Sep 11, 2009, 3:40:24 AM9/11/09
to TurboGears
> This shouldn't make a differenc, can you file a bug?

Done (but with a mistake, sorry!) #2358
Reply all
Reply to author
Forward
0 new messages