| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from third_party.handlebar import Handlebar | 5 from third_party.handlebar import Handlebar |
| 6 | 6 |
| 7 class TemplateDataSource(object): | 7 class TemplateDataSource(object): |
| 8 """This class fetches and compiles templates using the fetcher passed in with | 8 """This class fetches and compiles templates using the fetcher passed in with |
| 9 |cache_builder|. | 9 |cache_builder|. |
| 10 """ | 10 """ |
| 11 def __init__(self, cache_builder, base_paths): | 11 def __init__(self, cache_builder, base_paths): |
| 12 self._cache = cache_builder.build(self._LoadTemplate) | 12 self._cache = cache_builder.build(self._LoadTemplate) |
| 13 self._base_paths = base_paths | 13 self._base_paths = base_paths |
| 14 | 14 |
| 15 def _LoadTemplate(self, template): | 15 def _LoadTemplate(self, template): |
| 16 return Handlebar(template) | 16 return Handlebar(template) |
| 17 | 17 |
| 18 def Render(self, template_name, context): | 18 def Render(self, template_name, context): |
| 19 """This method will render a template named |template_name|, fetching all | 19 """This method will render a template named |template_name|, fetching all |
| 20 the partial templates needed from |self._cache|. Partials are retrieved | 20 the partial templates needed from |self._cache|. Partials are retrieved |
| 21 from the TemplateDataSource with the |get| method. | 21 from the TemplateDataSource with the |get| method. |
| 22 """ | 22 """ |
| 23 template = self.get(template_name) | 23 template = self.get(template_name) |
| 24 if not template: | 24 if not template: |
| 25 return '' | 25 return '' |
| 26 # TODO error handling | 26 # TODO error handling |
| 27 return template.render(context, {'templates': self}).text | 27 return template.render(context, {'partials': self}).text |
| 28 | 28 |
| 29 def __getitem__(self, key): | 29 def __getitem__(self, key): |
| 30 return self.get(key) | 30 return self.get(key) |
| 31 | 31 |
| 32 def get(self, key): | 32 def get(self, key): |
| 33 index = key.rfind('.html') | 33 index = key.rfind('.html') |
| 34 if index > 0: | 34 if index > 0: |
| 35 key = key[:index] | 35 key = key[:index] |
| 36 real_path = key + '.html' | 36 real_path = key + '.html' |
| 37 for base_path in self._base_paths: | 37 for base_path in self._base_paths: |
| 38 try: | 38 try: |
| 39 return self._cache.get(base_path + '/' + real_path) | 39 return self._cache.get(base_path + '/' + real_path) |
| 40 except: | 40 except: |
| 41 pass | 41 pass |
| 42 return None | 42 return None |
| OLD | NEW |