OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 import logging | 5 import logging |
6 | 6 |
7 from data_source_registry import CreateDataSources | 7 from data_source_registry import CreateDataSources |
8 from third_party.handlebar import Handlebar | 8 from third_party.handlebar import Handlebar |
9 from url_constants import GITHUB_BASE, EXTENSIONS_SAMPLES | 9 from url_constants import GITHUB_BASE, EXTENSIONS_SAMPLES |
10 | 10 |
11 | 11 |
12 class TemplateRenderer(object): | 12 class TemplateRenderer(object): |
13 '''Renders templates with the server's available data sources. | 13 '''Renders templates with the server's available data sources. |
14 ''' | 14 ''' |
15 | 15 |
16 def __init__(self, server_instance): | 16 def __init__(self, server_instance): |
17 self._server_instance = server_instance | 17 self._server_instance = server_instance |
18 | 18 |
19 def Render(self, template, request, data_sources=None, warn=True): | 19 def Render(self, |
| 20 template, |
| 21 request, |
| 22 data_sources=None, |
| 23 additional_context=None): |
20 '''Renders |template| using |request|. | 24 '''Renders |template| using |request|. |
| 25 |
21 Specify |data_sources| to only include the DataSources with the given names | 26 Specify |data_sources| to only include the DataSources with the given names |
22 when rendering the template. | 27 when rendering the template. |
23 Specify |warn| whether to warn (e.g. logging.warning) if there are template | 28 |
24 rendering errors. It may be useful to disable this when errors are expected, | 29 Specify |additional_context| to inject additional template context when |
25 for example when doing an initial page render to determine document title. | 30 rendering the template. |
26 ''' | 31 ''' |
27 assert isinstance(template, Handlebar), type(template) | 32 assert isinstance(template, Handlebar), type(template) |
28 render_context = self._CreateDataSources(request) | 33 render_context = self._CreateDataSources(request) |
29 if data_sources is not None: | 34 if data_sources is not None: |
30 render_context = dict((name, d) for name, d in render_context.iteritems() | 35 render_context = dict((name, d) for name, d in render_context.iteritems() |
31 if name in data_sources) | 36 if name in data_sources) |
32 render_context.update({ | 37 render_context.update({ |
33 'apps_samples_url': GITHUB_BASE, | 38 'apps_samples_url': GITHUB_BASE, |
34 'base_path': self._server_instance.base_path, | 39 'base_path': self._server_instance.base_path, |
35 'extensions_samples_url': EXTENSIONS_SAMPLES, | 40 'extensions_samples_url': EXTENSIONS_SAMPLES, |
36 'static': self._server_instance.base_path + 'static', | 41 'static': self._server_instance.base_path + 'static', |
37 }) | 42 }) |
| 43 if additional_context: |
| 44 render_context.update(additional_context) |
38 render_data = template.Render(render_context) | 45 render_data = template.Render(render_context) |
39 if warn and render_data.errors: | 46 return render_data.text, render_data.errors |
40 logging.error('Handlebar error(s) rendering %s:\n%s' % | |
41 (template._name, ' \n'.join(render_data.errors))) | |
42 return render_data.text | |
43 | 47 |
44 def _CreateDataSources(self, request): | 48 def _CreateDataSources(self, request): |
45 server_instance = self._server_instance | 49 server_instance = self._server_instance |
46 data_sources = CreateDataSources(server_instance, request=request) | 50 data_sources = CreateDataSources(server_instance, request=request) |
47 data_sources.update({ | 51 data_sources.update({ |
48 'api_list': server_instance.api_list_data_source_factory.Create(), | 52 'api_list': server_instance.api_list_data_source_factory.Create(), |
49 'apis': server_instance.api_data_source_factory.Create(request), | 53 'apis': server_instance.api_data_source_factory.Create(request), |
50 'samples': server_instance.samples_data_source_factory.Create(request), | 54 'samples': server_instance.samples_data_source_factory.Create(request), |
51 }) | 55 }) |
52 return data_sources | 56 return data_sources |
OLD | NEW |