helpers.py 929 Bytes
Newer Older
1 2
import os

3
from django.conf import settings
4
from mako.template import Template
5

Calen Pennington committed
6

7
def include_mustache_templates():
8
    mustache_dir = settings.COMMON_ROOT / 'templates' / 'discussion' / 'mustache'
9

10 11
    def is_valid_file_name(file_name):
        return file_name.endswith('.mustache')
12

13 14
    def read_file(file_name):
        return open(mustache_dir / file_name, "r").read().decode('utf-8')
15

16 17
    def template_id_from_file_name(file_name):
        return file_name.rpartition('.')[0]
18

19 20
    def process_mako(template_content):
        return Template(template_content).render_unicode()
21

22 23 24 25 26 27 28 29
    def make_script_tag(id, content):
        return u"<script type='text/template' id='{0}'>{1}</script>".format(id, content)

    return u'\n'.join(
        make_script_tag(template_id_from_file_name(file_name), process_mako(read_file(file_name)))
        for file_name in os.listdir(mustache_dir)
        if is_valid_file_name(file_name)
    )