@@ -77,49 +77,8 @@ def __str__(self):
7777 'items of class %(class)s' ) % {
7878 'action' : self .action , 'class' : self .klass }
7979
80- def find_template (dir , name , view ):
81- """ Find a template in the nominated dir
82- """
83- # find the source
84- if view :
85- filename = '%s.%s' % (name , view )
86- else :
87- filename = name
88-
89- # try old-style
90- src = os .path .join (dir , filename )
91- if os .path .exists (src ):
92- return (src , filename )
93-
94- # try with a .html or .xml extension (new-style)
95- for extension in '.html' , '.xml' :
96- f = filename + extension
97- src = os .path .join (dir , f )
98- if os .path .exists (src ):
99- return (src , f )
100-
101- # no view == no generic template is possible
102- if not view :
103- raise NoTemplate , 'Template file "%s" doesn\' t exist' % name
104-
105- # try _generic template for the view
106- generic = '_generic.%s' % view
107- src = os .path .join (dir , generic )
108- if os .path .exists (src ):
109- return (src , generic )
110-
111- # finally, try _generic.html
112- generic = generic + '.html'
113- src = os .path .join (dir , generic )
114- if os .path .exists (src ):
115- return (src , generic )
116-
117- raise NoTemplate ('No template file exists for templating "%s" '
118- 'with template "%s" (neither "%s" nor "%s")' % (name , view ,
119- filename , generic ))
120-
12180class LoaderBase :
122- """Base for engine-specific template Loader class."""
81+ """ Base for engine-specific template Loader class."""
12382 def precompileTemplates (self ):
12483 """ Go through a directory and precompile all the templates therein
12584 """
@@ -137,37 +96,31 @@ def precompileTemplates(self):
13796
13897 # remove extension
13998 filename = filename [:- len (extension )]
99+ self .load (filename )
140100
141- # load the template
142- if '.' in filename :
143- name , view = filename .split ('.' , 1 )
144- self .load (name , view )
145- else :
146- self .load (filename , None )
147-
148- def load (self , name , view = None ):
101+ def load (self , tplname ):
149102 """ Load template and return template object with render() method.
150103
151- "name" and "view" are used to select the template, which in
152- most cases will be "name.view". If "view" is None, then a
153- template called "name" will be selected.
104+ "tplname" is a template name. For filesystem loaders it is a
105+ filename without extensions, typically in the "classname.view"
106+ format.
107+ """
108+ raise NotImplementedError
154109
155- If the file "name.view" doesn't exist, we look for
156- "_generic.view" as a fallback.
110+ def check (self , name ):
111+ """ Check if template with the given name exists. Return None or
112+ a tuple (src, filename) that can be reused in load() method.
157113 """
158- # [ ] document default 'home' template and other special pages
159114 raise NotImplementedError
160115
161116 def __getitem__ (self , name ):
162117 """Special method to access templates by loader['name']"""
163- view = None
164- if '.' in name :
165- name , view = name .split ('.' , 1 )
166118 try :
167- return self .load (name , view )
119+ return self .load (name )
168120 except NoTemplate , message :
169121 raise KeyError , message
170122
123+
171124def get_loader (dir , engine_name ):
172125 if engine_name == 'chameleon' :
173126 import engine_chameleon as engine
@@ -700,7 +653,9 @@ def renderWith(self, name, **kwargs):
700653 req .update (kwargs )
701654
702655 # new template, using the specified classname and request
703- pt = self ._client .instance .templates .load (self .classname , name )
656+ # [ ] this code is too similar to client.renderContext()
657+ tplname = self ._client .selectTemplate (self .classname , name )
658+ pt = self ._client .instance .templates .load (tplname )
704659
705660 # use our fabricated request
706661 args = {
@@ -845,9 +800,8 @@ def history(self, direction='descending', dre=re.compile('^\d+$'),
845800 isinstance (self ._props [prop_n ], hyperdb .Link )):
846801 classname = self ._props [prop_n ].classname
847802 try :
848- template = find_template (self ._db .config .TEMPLATES ,
849- classname , 'item' )
850- if template [1 ].startswith ('_generic' ):
803+ template = self ._client .selectTemplate (classname , 'item' )
804+ if template .startswith ('_generic.' ):
851805 raise NoTemplate , 'not really...'
852806 except NoTemplate :
853807 pass
@@ -917,9 +871,9 @@ def history(self, direction='descending', dre=re.compile('^\d+$'),
917871 ) % locals ()
918872 labelprop = linkcl .labelprop (1 )
919873 try :
920- template = find_template ( self ._db . config . TEMPLATES ,
921- classname , 'item' )
922- if template [ 1 ] .startswith ('_generic' ):
874+ template = self ._client . selectTemplate ( classname ,
875+ 'item' )
876+ if template .startswith ('_generic. ' ):
923877 raise NoTemplate , 'not really...'
924878 hrefable = 1
925879 except NoTemplate :
@@ -1087,7 +1041,10 @@ def renderQueryForm(self):
10871041 '&@queryname=%s' % urllib .quote (name ))
10881042
10891043 # new template, using the specified classname and request
1090- pt = self ._client .instance .templates .load (req .classname , 'search' )
1044+ # [ ] the custom logic for search page doesn't belong to
1045+ # generic templating module (techtonik)
1046+ tplname = self ._client .selectTemplate (req .classname , 'search' )
1047+ pt = self ._client .instance .templates .load (tplname )
10911048 # The context for a search page should be the class, not any
10921049 # node.
10931050 self ._client .nodeid = None
0 commit comments