pyjade icon indicating copy to clipboard operation
pyjade copied to clipboard

UnboundLocalError : Local variable 'source' referenced before assignment

Open Mibou opened this issue 11 years ago • 0 comments

Hello,

I meet a problem with the load_template function.

If the template is a .jade :

if os.path.splitext(template_name)[1] in ('.jade',):
    try:
        source, display_name = self.load_template_source(template_name, template_dirs)
        source=process(source,filename=template_name,compiler=Compiler)
        origin = make_origin(display_name, self.load_template_source, template_name, template_dirs)
        template = get_template_from_string(source, origin, template_name)
    except NotImplementedError:
        template, origin = self.find_template(template_name, template_dirs)

Otherwise :

else:
    template, origin = self.find_template(template_name, template_dirs)

Here comes the problem because source variable may not be defined :

    if not hasattr(template, 'render'):
        try:
            template = get_template_from_string(process(source,filename=template_name,compiler=Compiler), origin, template_name)
        except TemplateDoesNotExist:
            # If compiling the template we found raises TemplateDoesNotExist,
            # back off to returning he source and display name for the template
            # we were asked to load. This allows for correct identification (later)
            # of the actual template that does not exist.
            return template, origin
    self.template_cache[key] = template
return self.template_cache[key], None

One case in which I had the problem was :

ipdb> template_name
u'admin/base_site.html'
ipdb> print hasattr(template, 'render')
False
ipdb> template,
(u'{% extends "admin/base.html" %}\n{% load i18n %}\n\n{% block extrahead %}\n    <script src="{{ STATIC_URL }}main/js/jquery-1.10.2.min.js" type="text/javascript"></script>\n    {% include \'autocomplete_light/static.html\' %}\n{% endblock %}\n\n{% block branding %}\n{% endblock %}',)

In this case, I get an exception saying local variable 'source' referenced before assignment

Mibou avatar Aug 05 '13 11:08 Mibou