Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for relative search path on the template loader #32

Closed
jpfarias opened this Issue · 2 comments

1 participant

@jpfarias

Hi!

I just tried to use the jinja2 loader in the same way as I do for the chameleon loader where it is able to find the assets relative to the current module's path but it didn't work.

Inspecting the source code I could figure a solution but I am not sure if this is a good solution, I'm submitting here for review. Once I have more time I will try to make some tests and send a pull request.

This is what the new Jinja2TemplateRenderer class looks like:

class Jinja2TemplateRenderer(object):
    '''Renderer for a jinja2 template'''
    template = None

    def __init__(self, info, environment):
        self.info = info
        self.environment = environment

    def implementation(self):
        return self.template

    @property
    def template(self):
        name = self.info.name
        name_with_package = name
        package = self.info.package
        if ':' not in name:
            name_with_package = '%s:%s' % (package.__name__, name)
        try:
            return self.environment.get_template(self.info.name)
        except TemplateNotFound:
            return self.environment.get_template(name_with_package)

    def __call__(self, value, system):
        try:
            system.update(value)
        except (TypeError, ValueError):
            ex = sys.exc_info()[1] # py2.5 - 3.2 compat
            raise ValueError('renderer was passed non-dictionary '
                             'as value: %s' % str(ex))
        return self.template.render(system)
@jpfarias

The code above won't pass the unit tests, I fixed it so they will work with the following code:

class Jinja2TemplateRenderer(object):
    '''Renderer for a jinja2 template'''
    template = None

    def __init__(self, info, environment):
        self.info = info
        self.environment = environment

    def implementation(self):
        return self.template

    @property
    def template(self):
        info = self.info
        name = info.name
        name_with_package = name
        if ':' not in name and hasattr(info, 'package') and info.package is not None:
            package = self.info.package
            name_with_package = '%s:%s' % (package.__name__, name)
        try:
            return self.environment.get_template(name)
        except TemplateNotFound:
            if name != name_with_package:
                return self.environment.get_template(name_with_package)
            else:
                raise

    def __call__(self, value, system):
        try:
            system.update(value)
        except (TypeError, ValueError):
            ex = sys.exc_info()[1] # py2.5 - 3.2 compat
            raise ValueError('renderer was passed non-dictionary '
                             'as value: %s' % str(ex))
        return self.template.render(system)

I did not implement any new tests though.

@jpfarias

Issued a pull request #33 that created a new issue. Closing this one.

@jpfarias jpfarias closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.