Common templates for multiple sites

1 view
Skip to first unread message

mulicheng

unread,
Dec 5, 2006, 11:47:13 AM12/5/06
to TurboGears
I'm working on a little sub project to allow a few of the sites I've
got running to share some common functionality. Part of the solution
requires a common template. (similar to a widget)

* I want the template to be defined in the common code base.
* I want the application to be able to extend or modify the template if
applicable (like modifying a widget)
* I want the template to be able to extend an application specific site
master or layout template.

I noticed a thread a while back (http://tinyurl.com/yb32ta) that talked
about loading templates from a dynamic source. There were some issues
and the proposed solution required adding a KID plugin to load the
template.

Would this be easier if I switch to Genshi? I'm not opposed to
upgrading anyway since that is the future.

Ideally I'd like to have something like this in my common controller
method:

class CommonController:
def __init__(self,mastertemplatename):
self.mastertemplatename=mastertemplatename

@expose()
def somecommontask(self,**kw):
template=pkg_resources.resource_string(__name__,'commontemplate.txt')
return
dict(tg_template=template,mastertemplatename=self.mastertemplatename)

Thoughts?

Thanks for any input.
-Dennis

Adam Jones

unread,
Dec 5, 2006, 1:38:49 PM12/5/06
to TurboGears

I posted something a little while back about adding themes to your
site. You can find it here:
http://groups.google.com/group/turbogears/msg/45da05797acb85dc

The concept seems similar to what you are trying to do, although your
usage of it would be more complicated.

hth,
-Adam

Alberto Valverde

unread,
Dec 5, 2006, 3:32:22 PM12/5/06
to turbo...@googlegroups.com
> Would this be easier if I switch to Genshi? I'm not opposed to
> upgrading anyway since that is the future.

Yes, it is easier with Genshi thanks to it's support for XInclude
(http://tinyurl.com/ya4j2k).

You can even use "py" directives to generate the include tags :)

Alberto

Dennis Muhlestein

unread,
Dec 5, 2006, 4:59:13 PM12/5/06
to TurboGears
On Dec 5, 1:32 pm, "Alberto Valverde" <albe...@toscat.net> wrote:
> > Would this be easier if I switch to Genshi? I'm not opposed to
> > upgrading anyway since that is the future.Yes, it is easier with Genshi thanks to it's support for XInclude

> (http://tinyurl.com/ya4j2k).
>
> You can even use "py" directives to generate the include tags :)

Yes, I had figured that out and was about to post my results. The
XInclude header does indeed make life easy on the template side. The
only thing I had to worry about is that turbogears doesn't seem to have
a way to specify more template paths. I did that manually.

Here is a simple controller that I can mount from any application.

class CommonController:
"""
Including site simply has to let controller know the path to
the site templates
and the name of the master template file.
"""
def __init__(self,template_dir,master_path):
self.t_dir=resource_filename(__name__,'templates')
self.t_loader=TemplateLoader([self.t_dir,template_dir])
self.master_path=master_path
@expose()
def login(self,**kw):
t=self.t_loader.load('login.html') # this template is in the common
template dir
return t.generate(master_path=self.master_path).render()


Thoughts?
Can you think of a better way to accomplish this?

-Dennis

Reply all
Reply to author
Forward
0 new messages