These are all good ways to use Cheetah, but I like to compile the
templates in init rather then externally via cheetah compile. You lose
the ability to have templates use other templates as base clases.
(Although you can just cheetah compile the base class)
If you are already using the normal Webkit.page you can just override
the writeContent method:
def writeContent(self):
#snip
if (user.comsci):
ClientList = clientpool.keys()
templateFile=os.path.join(os.path.dirname(self.serverSidePath()),'templates','WelcomeComSci.ctd')
t =
Template(file=templateFile,searchList=[{'user':user,'ClientList':ClientList}])
else:
dataStats=CSData.summaryStats(user)
templateFile=os.path.join(os.path.dirname(self.serverSidePath()),'templates','Welcome.ctd')
t =
Template(file=templateFile,searchList=[{'user':user,dataStats])
self._actionDesc = '%s Messages for %s ' %
(len(boxContents),user.name)
self.writeln(t)
This will recompile the templates each run so you can play with them
It's easy to precompile them also:
def __init__(self, config,sect):
self.compileTemplates()
def compileTemplates(self):
self.clientTemplate = Template(file="siteTemplate.tmpl")
self.employeeTemplate = Template(file="empTemplate.tmpl")
def writeContent(self):
#self.compileTemplates() #<-- uncomment when developing
if user.isClient:
t = self.clientTemplate
t.data = getClientData()
else:
t = self.employeeTemplate
t.clientList = getCleintList()
t.generic = getUniversalData()
self.write(t)
A final idea is to use a cheetah template to populate a function called
in another template:
SitePage.tmpl =
<head><title></title></head><body>TOP HEADERS GOES HERE
<div class=main>
$pageContents
</div>
FOOTER
</body></html>
in the SitePage:
def writeHTML(self):
t = Template('SitePage.tmpl')
t.pageContents = self.getContents()
self.write(t)
def getContents(self):
if isData:
t = Template('pageData.tmpl')
return str(t)
else:
return ' This is not a page '
You can combine these to write a complex template system that will
strain even Pythons ability to write understanable code.
-Aaron
deelan wrote:
> Peter Lyons wrote:
>
>> (...) The only other idea I had was to override __import__ in the
>> __init__.py module, and there I think you might be able to do
>> something less brutal by hooking into the import process, but I'm not
>> enough of a pythonista yet to investigate that one.
>
>
> i just tried this from python console:
> m = __import__('socket', globals(), locals(), [])
>
> this import "socket" module a store in m. being a python newbie
> too i'm wondering is this can be used instead of exec(<python code>).
>
> __import__:
>
> '' This function is invoked by the import statement. It mainly exists
> so that you can replace it with another function that has a compatible
> interface, in order to change the semantics of the import statement. ''
>
> http://www.python.org/doc/current/lib/built-in-funcs.html
>
>> I've only been using this approach for a brief period, and I like
>> it so far. I like that fact that I can change the way a page looks
>> by changing the template it inherits from without messing with the
>> business logic. The pure inheritence approach to combining Cheetah
>> and Webware was too confusing for me.
>
> same here, i tried a couple of different approaches but i never
> felt confortable with them. this makes more sense for me.
>
>> Big hunks of HTML inside a servlet just won't fly for me, and I hate
>> trying to do anything non-presentation with Cheetah syntax, so this
>> approach keeps me pretty happy.
>
> here at work i would like to introduce the "graphics guy" to valid
> HTML and CSS layouts so i have to pass clean templates to him. this could
> help the process a lot.
>
>> If I find anything particularly nice about this approach, or any
>> major flaws, I'll post them to the Cheetah list.
>
> thanks.
>
> later,
> deelan
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: VM Ware
> With VMware you can run multiple operating systems on a single machine.
> WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
> same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
> _______________________________________________
> Webware-discuss mailing list
> Webware-discuss@...
> https://lists.sourceforge.net/lists/listinfo/webware-discuss
--
-Aaron
http://www.MetroNY.com/
"I don't know what's wrong with my television set. I was getting
C-Span and the Home Shopping Network on the same station.
I actually bought a congressman."
- Bruce Baum
|