Package oort :: Package util :: Module templating
[hide private]
[frames] | no frames]

Source Code for Module oort.util.templating

 1  # Based on design in: 
 2  #   <http://www.turbogears.org/docs/plugins/template.html> 
 3  #   <http://projects.dowski.com/view/buffet> 
 4   
 5  import pkg_resources 
 6   
 7  available_engines = dict([ (entry_point.name, entry_point.load()) 
 8          for entry_point in pkg_resources. 
 9                  iter_entry_points('python.templating.engines') ]) 
10   
11 -class TemplateEngineManager(object):
12
13 - def __init__(self, extra_vars_func=None, engine_opts=None):
14 self.extra_vars_func = extra_vars_func 15 self.engine_opts = engine_opts 16 self.engines = {}
17
18 - def get_engine_and_path(self, path, default_engine=None):
19 if not ':' in path: 20 dotted_path = path 21 engine_name = default_engine 22 else: 23 engine_name, dotted_path = path.split(':', 1) 24 engine = self._get_engine(engine_name) 25 return engine, dotted_path
26
27 - def render_template(self, path, data, format, fragment=False):
28 engine, dotted_path = self.get_engine_and_path(path) 29 result = engine.render( 30 data, format=format, fragment=fragment, template=dotted_path) 31 return result
32
33 - def _get_engine(self, engine_name):
34 engine = self.engines.get(engine_name) 35 if not engine: 36 Engine = available_engines.get(engine_name, None) 37 if not Engine: 38 msg = 'No engine for "%s" found. Please install it.' 39 raise TemplateEngineMissing(msg % (engine_name,)) 40 self.engines[engine_name] = \ 41 engine = Engine(self.extra_vars_func, self.engine_opts) 42 return engine
43
44 -class TemplateEngineMissing(Exception):
45 pass
46