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
3 # found in the LICENSE file.
5 from data_source_registry
import CreateDataSources
6 from third_party
.handlebar
import Handlebar
7 from url_constants
import GITHUB_BASE
, EXTENSIONS_SAMPLES
10 class TemplateRenderer(object):
11 '''Renders templates with the server's available data sources.
14 def __init__(self
, server_instance
):
15 self
._server
_instance
= server_instance
21 additional_context
=None):
22 '''Renders |template| using |request|.
24 Specify |data_sources| to only include the DataSources with the given names
25 when rendering the template.
27 Specify |additional_context| to inject additional template context when
28 rendering the template.
30 assert isinstance(template
, Handlebar
), type(template
)
31 render_context
= self
._CreateDataSources
(request
)
32 if data_sources
is not None:
33 render_context
= dict((name
, d
) for name
, d
in render_context
.iteritems()
34 if name
in data_sources
)
35 render_context
.update({
36 'apps_samples_url': GITHUB_BASE
,
37 'base_path': self
._server
_instance
.base_path
,
38 'extensions_samples_url': EXTENSIONS_SAMPLES
,
39 'static': self
._server
_instance
.base_path
+ 'static',
41 if additional_context
:
42 render_context
.update(additional_context
)
43 render_data
= template
.Render(render_context
)
44 return render_data
.text
, render_data
.errors
46 def _CreateDataSources(self
, request
):
47 server_instance
= self
._server
_instance
48 data_sources
= CreateDataSources(server_instance
, request
=request
)
50 'api_list': server_instance
.api_list_data_source_factory
.Create(),
51 'apis': server_instance
.api_data_source_factory
.Create(request
),
52 'samples': server_instance
.samples_data_source_factory
.Create(request
),