Mercurial > libervia-backend
comparison src/tools/common/template.py @ 2159:5734b0994cf0
core (tools/common): template renderer first draft:
jinja integration is being rewritten to be used not only in Libervia, this is a first step which put the renderer in backend/frontends tools.
The renderer is mainly HTML oriented, but may be used for other files.
sat_templates dependency is needed to use it.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 21 Feb 2017 21:01:39 +0100 |
parents | |
children | f472179305a1 |
comparison
equal
deleted
inserted
replaced
2158:970a348d3fe9 | 2159:5734b0994cf0 |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """ template generation """ | |
21 | |
22 from sat.core.constants import Const as C | |
23 from sat.core import exceptions | |
24 from sat.core.log import getLogger | |
25 log = getLogger(__name__) | |
26 import os.path | |
27 try: | |
28 import sat_templates | |
29 except ImportError: | |
30 raise exceptions.MissingModule(u'sat_templates module is not available, please install it or check your path to use template engine') | |
31 else: | |
32 sat_templates # to avoid pyflakes warning | |
33 | |
34 try: | |
35 import jinja2 | |
36 except: | |
37 raise exceptions.MissingModule(u'Missing module jinja2, please install it from http://jinja.pocoo.org or with pip install jinja2') | |
38 | |
39 | |
40 class Renderer(object): | |
41 | |
42 def __init__(self, host): # , template_dir=None): | |
43 self.host = host | |
44 self.base_dir = os.path.dirname(sat_templates.__file__) | |
45 self.theme = u'default' # FIXME: temporary, template should be selected in render() | |
46 self.env = jinja2.Environment( | |
47 loader=jinja2.PackageLoader('sat_templates', self.theme), | |
48 autoescape=jinja2.select_autoescape(['html', 'xhtml', 'xml']), | |
49 trim_blocks=True, | |
50 lstrip_blocks=True, | |
51 ) | |
52 # we want to have access to SàT constants in templates | |
53 self.env.globals[u'C'] = C | |
54 | |
55 def render(self, template_path, theme=u"default", css_file=u"style.css", css_inline=False, **kwargs): | |
56 """render a template | |
57 | |
58 @param template_path(unicode): path of the template to render (e.g. blog/articles.html) | |
59 @param theme(unicode): template theme | |
60 @param css_file(unicode): path to CSS file (relative to template dir, or absolute) | |
61 @param css_inline(bool): if True, CSS will be embedded in the HTML page | |
62 @param **kwargs: variable to transmit to the template | |
63 """ | |
64 | |
65 # TODO: handle theme | |
66 template = self.env.get_template(template_path) | |
67 if css_inline: | |
68 css_file_path = os.path.join(self.getStaticDir(template_path), css_file) | |
69 with open(css_file_path, 'r') as f: | |
70 kwargs[u"css_content"] = f.read() | |
71 return template.render(theme=theme, css_file=css_file, css_inline=css_inline, **kwargs) | |
72 | |
73 def getStaticDir(self, template_path): | |
74 template_base = template_path.split(u'/')[0] | |
75 return os.path.join(self.base_dir, self.theme, template_base, "static") |