1. volksman
  2. django-flext

Source

django-flext / django_flext / loaders.py

from django.template import TemplateDoesNotExist
from django.template.loader import BaseLoader
from django.template.loaders import filesystem
import django_flext


class FlexibleLoader(BaseLoader):
    is_usable = True

    def __init__(self, *args, **kwargs):
        super(FlexibleLoader, self).__init__(*args, **kwargs)
        self.reset()

    def load_template_source(self, template_name, template_dirs=None):
        """
        Returns a tuple containing the source and origin for the given template
        name.
        
        """
        print template_dirs
        if template_dirs is None:
            template_dirs_list = django_flext.get_dirs()
            print template_dirs_list
        else:
            template_dirs_list = [template_dirs]
        if not template_dirs_list:
            raise TemplateDoesNotExist('No template directories provided.')
        for template_dirs in template_dirs_list:
            # Each template_dirs must either be a list of template directories
            # or a two part tuple where the first is a Loader and the second is
            # a list of template directories.
            try:
                if not isinstance(template_dirs[0], BaseLoader):
                    raise TypeError
                loader, template_dirs = template_dirs
            except (TypeError, ValueError):
                loader = filesystem.Loader()
            try:
                return loader.load_template_source(template_name,
                                                   template_dirs)
            except TemplateDoesNotExist:
                continue
        raise TemplateDoesNotExist('No matching source found.')
    load_template_source.is_usable = True

    def reset(self):
        """
        Resets the state maintained by the loader instance.

        """
        django_flext.reset()