Can I set a controllers template inside the controller (instead of inside expose)

0 views
Skip to first unread message

kerinin

unread,
Jul 1, 2009, 7:05:20 PM7/1/09
to TurboGears
In TG1, it was possible to include an argument 'tg_template' in the
return dict and that argument would determine which template would be
used - is this possible in TG2?

Thanks
-Ryan

Diez B. Roggisch

unread,
Jul 2, 2009, 5:45:33 AM7/2/09
to turbo...@googlegroups.com
kerinin schrieb:

> In TG1, it was possible to include an argument 'tg_template' in the
> return dict and that argument would determine which template would be
> used - is this possible in TG2?

Look for the "override_template"-function. Don't forget to prefix the
template with the engine of your choice.

override_template("genshi:foo.bar.baz")

Diez

Ryan Michael

unread,
Jul 2, 2009, 12:17:35 PM7/2/09
to TurboGears
Thanks - that did it!

I can't find that function at all in the documentation - I assume the
docs are still a work in progress, or am I not looking in the right
place?

Diez B. Roggisch

unread,
Jul 3, 2009, 4:19:59 AM7/3/09
to turbo...@googlegroups.com
Ryan Michael schrieb:

> Thanks - that did it!
>
> I can't find that function at all in the documentation - I assume the
> docs are still a work in progress, or am I not looking in the right
> place?

They are a work in progress (too slow progress unfortunately), but that
certainly should be in there.

And it is

http://turbogears.org/2.0/docs/modules/tgdecorators.html

However, I found this only because I manipulated the URL - so it appears
as if there is an error in the doc-generation-process.

Diez

spam&eggs

unread,
Jul 3, 2009, 10:17:39 AM7/3/09
to TurboGears
I just used this solution myself, and wanted to make this even more
explicit - so other people will have an easier time doing this.


from tg.decorators import override_template

# within the controller function, if the function is "save"

override_template(self.save, "genshi:path.to.template.name")


##############################
# here is a little more code to make it even more understandable:

from tg.decorators import override_template

class JobController(BaseController):
#...

@expose('path.to.template.save')
def save(self, **kw):
if self.check_for_conflicts(kw):
override_template(self.save,
'genshi:path.to.template.conflict_list')
# note: check_for_conflicts added info to kw about
conflicts found
return kw
#... normal save
return dict(...)


-Laurin
Reply all
Reply to author
Forward
0 new messages