[SQLObject] Plugging in a new templating engine
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Peter W. <pwi...@th...> - 2003-04-13 08:01:31
|
Hi, I've had a few discussions with some of the HTML folks I work with a some of them were a little wary of them dealing with Python code in the templates so I've been looking at the possibility of hooking other template engines into Draco that don't need Python code - these guys are used to various template kits sitting on top of Perl CGI's. Not having done a lot of Python before I had a quick look around and found what seems to be a nice simple template tool in SimpleTAL (http://www.owlfish.com/software/simpleTAL/). I've been able to plug it into Draco with a minimum of fuss, just a new import statement and a few lines changed to dracohandler.py, I've made no attempt at this stage to support multiple types of templates and switch between them but doing that would be quite simple I'd think. Also the really simple change I did knocks out support for tag rewriters but could be put back in easily I'm sure. Does this sound like something that could make sense in Draco - with a config option of some sort to choose the template engine to use. from simpletal import simpleTAL, simpleTALES and at line 471 (needs to have support for using normal Draco templates as well) if template: #environment = DracoEnvironment() context = simpleTALES.Context() if handler: #environment.update(handler) for key,value in handler.items(): context.addGlobal(key, value) response.addHeader('Cache-Control', 'no-cache') #rewriters = rewritemanager.rewriters() #parser = Parser(document, opener, environment, rewriters) #output = parser.parse() response.setBuffering(1) #response.write(output) templateFile = opener.open(document) template = simpleTAL.compileHTMLTemplate(templateFile) templateFile.close() template.expand(context, response) This takes a template that looks like: <html> <body> <h1 tal:content="title">Title</h1> <h2 tal:condition="username">Welcome back <b tal:replace="username">Username here</b></h2> <p> Links for today are: <ul> <li tal:repeat="news hotItems"><a href="http://www.sample.org/" tal:attributes="href news/link" tal:content="news/title">News item</a></li> </ul> </p> </body> </html> and when the __handler__.py has something like: def test(self, path, args): self["title"] = "Daily links" self["username"] = "peter w" self["hotItems"] = [ {"link": "http://www.yahoo.com", "title": "Yahoo!"}, {"link": "http://www.google.com", "title": "Google"}, {"link": "http://www.apple.com", "title": "Apple"} ] it generates: <html> <body> <h1>Daily links</h1> <h2>Welcome back peter w</h2> <p> Links for today are: <ul> <li><a href="http://www.yahoo.com">Yahoo!</a></li><li><a href="http://www.google.com">Google</a></li><li><a href="http://www.apple.com">Apple</a></li> </ul> </p> </body> </html> Interesting? regards, -- peter w. |