Skip to content

Commit cffc4d8

Browse files
committed
templating: Move common TAL methods to TALLoaderBase class
1 parent f11e3d9 commit cffc4d8

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Features:
1616
- Experimental proof of concept code for Jinja2 templating engine.
1717
Select 'jinja2' template_engine in config and place templates into
1818
html/jinja2 to play with (anatoly techtonik)
19+
- Introducing Template Loader API (anatoly techtonik)
1920

2021
Fixed:
2122

roundup/cgi/engine_chameleon.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@
55
import os.path
66
import chameleon
77

8-
from roundup.cgi.templating import StringIO, context, LoaderBase
8+
from roundup.cgi.templating import StringIO, context, TALLoaderBase
99

10-
class Loader(LoaderBase):
10+
class Loader(TALLoaderBase):
1111
def __init__(self, dir):
1212
self.dir = dir
1313
self.loader = chameleon.PageTemplateLoader(dir)
1414

15-
def check(self, name):
16-
for extension in ['', '.html', '.xml']:
17-
f = name + extension
18-
src = os.path.join(self.dir, f)
19-
if os.path.exists(src):
20-
return (src, f)
21-
2215
def load(self, tplname):
23-
src, filename = self.check(tplname)
16+
src, filename = self._find(tplname)
2417
return RoundupPageTemplate(self.loader.load(src))
2518

2619
class RoundupPageTemplate(object):

roundup/cgi/engine_zopetal.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,22 @@
88
import os
99
import os.path
1010

11-
from roundup.cgi.templating import StringIO, context, translationService, LoaderBase
11+
from roundup.cgi.templating import StringIO, context, translationService, TALLoaderBase
1212
from roundup.cgi.PageTemplates import PageTemplate, GlobalTranslationService
1313
from roundup.cgi.PageTemplates.Expressions import getEngine
1414
from roundup.cgi.TAL import TALInterpreter
1515

1616
GlobalTranslationService.setGlobalTranslationService(translationService)
1717

18-
class Loader(LoaderBase):
18+
class Loader(TALLoaderBase):
1919
templates = {}
2020

2121
def __init__(self, dir):
2222
self.dir = dir
2323

24-
def check(self, name):
25-
for extension in ['', '.html', '.xml']:
26-
f = name + extension
27-
src = os.path.join(self.dir, f)
28-
if os.path.exists(src):
29-
return (src, f)
30-
3124
def load(self, tplname):
3225
# find the source
33-
src, filename = self.check(tplname)
26+
src, filename = self._find(tplname)
3427

3528
# has it changed?
3629
try:

roundup/cgi/templating.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33

44
todo = """
5+
- Document parameters to Template.render() method
56
- Add tests for Loader.load() method
67
- Most methods should have a "default" arg to supply a value
78
when none appears in the hyperdb or request.
@@ -88,7 +89,43 @@ def precompile(self):
8889
""" This method may be called when tracker is loaded to precompile
8990
templates that support this ability.
9091
"""
91-
# [ ] move implementation out of API
92+
pass
93+
94+
def load(self, tplname):
95+
""" Load template and return template object with render() method.
96+
97+
"tplname" is a template name. For filesystem loaders it is a
98+
filename without extensions, typically in the "classname.view"
99+
format.
100+
"""
101+
raise NotImplementedError
102+
103+
def check(self, name):
104+
""" Check if template with the given name exists. Should return
105+
false if template can not be found.
106+
"""
107+
raise NotImplementedError
108+
109+
class TALLoaderBase(LoaderBase):
110+
""" Common methods for the legacy TAL loaders."""
111+
112+
def __init__(self, dir):
113+
self.dir = dir
114+
115+
def _find(self, name):
116+
""" Find template, return full path and filename of the
117+
template if it is found, None otherwise."""
118+
for extension in ['', '.html', '.xml']:
119+
f = name + extension
120+
src = os.path.join(self.dir, f)
121+
if os.path.exists(src):
122+
return (src, f)
123+
124+
def check(self, name):
125+
return bool(self._find(name))
126+
127+
def precompile(self):
128+
""" Precompile templates in load directory by loading them """
92129
for filename in os.listdir(self.dir):
93130
# skip subdirs
94131
if os.path.isdir(filename):
@@ -105,24 +142,8 @@ def precompile(self):
105142
filename = filename[:-len(extension)]
106143
self.load(filename)
107144

108-
def load(self, tplname):
109-
""" Load template and return template object with render() method.
110-
111-
"tplname" is a template name. For filesystem loaders it is a
112-
filename without extensions, typically in the "classname.view"
113-
format.
114-
"""
115-
raise NotImplementedError
116-
117-
def check(self, name):
118-
""" Check if template with the given name exists. Return None or
119-
a tuple (src, filename) that can be reused in load() method.
120-
"""
121-
raise NotImplementedError
122-
123145
def __getitem__(self, name):
124146
"""Special method to access templates by loader['name']"""
125-
# [ ] not sure if it is needed for anything except TAL templates
126147
try:
127148
return self.load(name)
128149
except NoTemplate, message:

0 commit comments

Comments
 (0)