summaryrefslogtreecommitdiffstats
path: root/nikola/plugins/template_jinja.py
blob: 0893cf73d109a9f1d1097949311c2e5817adb8c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Jinja template handlers"""

import os
import jinja2

from nikola.plugin_categories import TemplateSystem


class JinjaTemplates(TemplateSystem):
    """Wrapper for Jinja2 templates."""

    name = "jinja"
    lookup = None

    def set_directories(self, directories):
        """Createa  template lookup."""
        self.lookup = jinja2.Environment(loader=jinja2.FileSystemLoader(
            directories,
            encoding='utf-8',
            ))

    def render_template(self, template_name, output_name, context):
        """Render the template into output_name using context."""

        template = self.lookup.get_template(template_name)
        output = template.render(**context)
        if output_name is not None:
            try:
                os.makedirs(os.path.dirname(output_name))
            except:
                pass
            with open(output_name, 'w+') as output:
                output.write(output.encode('utf8'))
        return output

    def template_deps(self, template_name):
        # FIXME: unimplemented
        return []