Mercurial > p > roundup > code
changeset 4749:0421390b3094
templating: Move common TAL methods to TALLoaderBase class
| author | anatoly techtonik <techtonik@gmail.com> |
|---|---|
| date | Tue, 22 Jan 2013 17:29:06 +0300 |
| parents | e339583eae8e |
| children | 12b029655c05 |
| files | CHANGES.txt roundup/cgi/engine_chameleon.py roundup/cgi/engine_zopetal.py roundup/cgi/templating.py |
| diffstat | 4 files changed, 45 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGES.txt Mon Jan 21 23:51:39 2013 +0300 +++ b/CHANGES.txt Tue Jan 22 17:29:06 2013 +0300 @@ -16,6 +16,7 @@ - Experimental proof of concept code for Jinja2 templating engine. Select 'jinja2' template_engine in config and place templates into html/jinja2 to play with (anatoly techtonik) +- Introducing Template Loader API (anatoly techtonik) Fixed:
--- a/roundup/cgi/engine_chameleon.py Mon Jan 21 23:51:39 2013 +0300 +++ b/roundup/cgi/engine_chameleon.py Tue Jan 22 17:29:06 2013 +0300 @@ -5,22 +5,15 @@ import os.path import chameleon -from roundup.cgi.templating import StringIO, context, LoaderBase +from roundup.cgi.templating import StringIO, context, TALLoaderBase -class Loader(LoaderBase): +class Loader(TALLoaderBase): def __init__(self, dir): self.dir = dir self.loader = chameleon.PageTemplateLoader(dir) - def check(self, name): - for extension in ['', '.html', '.xml']: - f = name + extension - src = os.path.join(self.dir, f) - if os.path.exists(src): - return (src, f) - def load(self, tplname): - src, filename = self.check(tplname) + src, filename = self._find(tplname) return RoundupPageTemplate(self.loader.load(src)) class RoundupPageTemplate(object):
--- a/roundup/cgi/engine_zopetal.py Mon Jan 21 23:51:39 2013 +0300 +++ b/roundup/cgi/engine_zopetal.py Tue Jan 22 17:29:06 2013 +0300 @@ -8,29 +8,22 @@ import os import os.path -from roundup.cgi.templating import StringIO, context, translationService, LoaderBase +from roundup.cgi.templating import StringIO, context, translationService, TALLoaderBase from roundup.cgi.PageTemplates import PageTemplate, GlobalTranslationService from roundup.cgi.PageTemplates.Expressions import getEngine from roundup.cgi.TAL import TALInterpreter GlobalTranslationService.setGlobalTranslationService(translationService) -class Loader(LoaderBase): +class Loader(TALLoaderBase): templates = {} def __init__(self, dir): self.dir = dir - def check(self, name): - for extension in ['', '.html', '.xml']: - f = name + extension - src = os.path.join(self.dir, f) - if os.path.exists(src): - return (src, f) - def load(self, tplname): # find the source - src, filename = self.check(tplname) + src, filename = self._find(tplname) # has it changed? try:
--- a/roundup/cgi/templating.py Mon Jan 21 23:51:39 2013 +0300 +++ b/roundup/cgi/templating.py Tue Jan 22 17:29:06 2013 +0300 @@ -2,6 +2,7 @@ """ todo = """ +- Document parameters to Template.render() method - Add tests for Loader.load() method - Most methods should have a "default" arg to supply a value when none appears in the hyperdb or request. @@ -88,7 +89,43 @@ """ This method may be called when tracker is loaded to precompile templates that support this ability. """ - # [ ] move implementation out of API + pass + + def load(self, tplname): + """ Load template and return template object with render() method. + + "tplname" is a template name. For filesystem loaders it is a + filename without extensions, typically in the "classname.view" + format. + """ + raise NotImplementedError + + def check(self, name): + """ Check if template with the given name exists. Should return + false if template can not be found. + """ + raise NotImplementedError + +class TALLoaderBase(LoaderBase): + """ Common methods for the legacy TAL loaders.""" + + def __init__(self, dir): + self.dir = dir + + def _find(self, name): + """ Find template, return full path and filename of the + template if it is found, None otherwise.""" + for extension in ['', '.html', '.xml']: + f = name + extension + src = os.path.join(self.dir, f) + if os.path.exists(src): + return (src, f) + + def check(self, name): + return bool(self._find(name)) + + def precompile(self): + """ Precompile templates in load directory by loading them """ for filename in os.listdir(self.dir): # skip subdirs if os.path.isdir(filename): @@ -105,24 +142,8 @@ filename = filename[:-len(extension)] self.load(filename) - def load(self, tplname): - """ Load template and return template object with render() method. - - "tplname" is a template name. For filesystem loaders it is a - filename without extensions, typically in the "classname.view" - format. - """ - raise NotImplementedError - - def check(self, name): - """ Check if template with the given name exists. Return None or - a tuple (src, filename) that can be reused in load() method. - """ - raise NotImplementedError - def __getitem__(self, name): """Special method to access templates by loader['name']""" - # [ ] not sure if it is needed for anything except TAL templates try: return self.load(name) except NoTemplate, message:
