Beispiel #1
0
        def load_partial(name):
            template = partials.get(name)

            if template is None:
                raise TemplateNotFoundError(
                    "Name %s not found in partials: %s" %
                    (repr(name), type(partials)))

            # RenderEngine requires that the return value be unicode.
            return self._to_unicode_hard(template)
Beispiel #2
0
    def _find_path_required(self, search_dirs, file_name):
        """
        Return the path to a template with the given file name.

        """
        path = self._find_path(search_dirs, file_name)

        if path is None:
            raise TemplateNotFoundError('File %s not found in dirs: %s' %
                                        (repr(file_name), repr(search_dirs)))

        return path
Beispiel #3
0
        def load_partial(name):
            # TODO: consider using EAFP here instead.
            #     http://docs.python.org/glossary.html#term-eafp
            #   This would mean requiring that the custom partial loader
            #   raise a KeyError on name not found.
            template = partials.get(name)
            if template is None:
                raise TemplateNotFoundError("Name %s not found in partials: %s" %
                                            (repr(name), type(partials)))

            # RenderEngine requires that the return value be unicode.
            return self._to_unicode_hard(template)
Beispiel #4
0
        def load_partial(name, location):
            # TODO: consider using EAFP here instead.
            #     http://docs.python.org/glossary.html#term-eafp
            #   This would mean requiring that the custom partial loader
            #   raise a KeyError on name not found.
            template = partials.get(name)
            if template is None:
                msg = "Name {} not found in partials: {}".format(
                    repr(name), type(partials))
                raise TemplateNotFoundError(msg, location)

            # RenderEngine requires that the return value be str.
            assert isinstance(template, str)
            return template
Beispiel #5
0
    def _render_object(self, obj, *context, **kwargs):
        """
        Render the template associated with the given object.

        """
        loader = self._make_loader()

        # TODO: consider an approach that does not require using an if
        #   block here.  For example, perhaps this class's loader can be
        #   a SpecLoader in all cases, and the SpecLoader instance can
        #   check the object's type.  Or perhaps Loader and SpecLoader
        #   can be refactored to implement the same interface.
        try:
            if isinstance(obj, TemplateSpec):
                loader = SpecLoader(loader)
                template = loader.load(obj)
            else:
                template = loader.load_object(obj)
        except LocatorNotFoundError as e:
            raise TemplateNotFoundError(str(e), None)

        context = [obj] + list(context)

        return self._render_string(template, '<obj>', *context, **kwargs)
Beispiel #6
0
 def load_template(template_name, location):
     try:
         return loader.load_name(template_name)
     except LocatorNotFoundError as e:
         raise TemplateNotFoundError(str(e), location)