Browse Source

[#100] First implementation of a Template module using jinja2 for template rendering

Roland Haroutiounian 8 years ago
parent
commit
4bc15b1f6d
3 changed files with 31 additions and 0 deletions
  1. 29
    0
      Template/Loader.py
  2. 1
    0
      Template/__init__.py
  3. 1
    0
      requirements.txt

+ 29
- 0
Template/Loader.py View File

@@ -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)

+ 1
- 0
Template/__init__.py View File

@@ -0,0 +1 @@
1
+__author__ = 'roland'

+ 1
- 0
requirements.txt View File

@@ -2,3 +2,4 @@ PyMySQL==0.6.7
2 2
 mosql==0.11
3 3
 pep8==1.6.2
4 4
 pylint==1.4.4
5
+jinja2

Loading…
Cancel
Save