Beispiel #1
0
class Loader(BaseLoader):
    is_usable = True
    fs_loader = FilesystemLoader()

    def _get_template_vars(self, template_name):
        app_name, template_name = template_name.split(":", 1)
        try:
            app_dir = path.dirname(get_app(app_name).__file__)
            if path.split(app_dir)[-1] == 'models':
                app_dir, _ = path.split(app_dir)
            template_dir = path.abspath(path.join(app_dir, 'templates'))
        except ImproperlyConfigured:
            raise TemplateDoesNotExist()
        return template_name, template_dir

    def load_template_source(self, template_name, template_dirs=None):
        """
        Template loader that only serves templates from specific app's template directory.

        Works for template_names in format app_label:some/template/name.html
        """
        if ":" not in template_name:
            raise TemplateDoesNotExist()
        template_name, template_dir = self._get_template_vars(template_name)
        if not path.isdir(template_dir):
            raise TemplateDoesNotExist()

        return self.fs_loader.load_template_source(template_name,
                                                   template_dirs=[template_dir])
Beispiel #2
0
 def __init__(self):
     MakoLoader.__init__(self, FilesystemLoader())
Beispiel #3
0
 def __init__(self, *args):
     MakoLoader.__init__(self, FilesystemLoader(*args))
Beispiel #4
0
 def __init__(self, *args):
     MakoLoader.__init__(self, FilesystemLoader(*args))  # lint-amnesty, pylint: disable=no-value-for-parameter
Beispiel #5
0
# -*- coding: utf-8 -*-

from django import template
from django.template import TemplateSyntaxError
from django.template.loaders.filesystem import Loader as FilesystemLoader

_loader = FilesystemLoader()

register = template.Library()


def do_include_raw(parser, token):
    """
    Performs a template include without parsing the context, just dumps the template in.
    """
    bits = token.split_contents()
    if len(bits) != 2:
        raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[
            0]

    template_name = bits[1]
    if template_name[0] in ('"',
                            "'") and template_name[-1] == template_name[0]:
        template_name = template_name[1:-1]

    source, path = _loader.load_template_source(template_name)
    return template.TextNode(source)


register.tag("include_raw", do_include_raw)