|
@@ -0,0 +1,29 @@
|
|
1
|
+#-*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import jinja2
|
|
4
|
+import settings_local
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+class TemplateLoader(object):
|
|
8
|
+
|
|
9
|
+ ## @brief Initializes a template loader
|
|
10
|
+ #
|
|
11
|
+ # @param search_path str : the base path from which the templates are searched. To use absolute paths, you can set
|
|
12
|
+ # it to the root "/". By default, it will be the root of the project, defined in the
|
|
13
|
+ # settings of the application
|
|
14
|
+ # @param follow_links bool : indicates whether or not to follow the symbolic links (default: True)
|
|
15
|
+ def __init__(self, search_path=settings_local.base_path, follow_links=True):
|
|
16
|
+ self.search_path = search_path
|
|
17
|
+ self.follow_links = follow_links
|
|
18
|
+
|
|
19
|
+ ## @brief Renders a HTML content of a template
|
|
20
|
+ #
|
|
21
|
+ # @param template_file str : path to the template file (starting from the base path used to instanciate the
|
|
22
|
+ # TemplateLoader)
|
|
23
|
+ # @param template_vars dict : parameters to be used in the template
|
|
24
|
+ # @return str. String containing the HTML output of the processed templated
|
|
25
|
+ def render_to_html(self, template_file, template_vars):
|
|
26
|
+ loader = jinja2.FileSystemLoader(searchpath=self.search_path, followlinks=self.follow_links)
|
|
27
|
+ environment = jinja2.Environment(loader=loader)
|
|
28
|
+ template = environment.get_template(template_file)
|
|
29
|
+ return template.render(template_vars)
|