コード例 #1
0
    def resolve_template(self, template):
        """Takes a template and tries to return back the appropriate
        Jinja template object.
        If an explicit Django template object is passed, do nothing."""
      
        # dirty rotten hack:
        # there's one Django test that fails when sent to a Jinja template,
        #   (d.c.auth.tests.views.ChangePasswordTest.test_password_change_succeeds)
        #   it's a test-specific template (not used elsewhere) that simply contains
        #   `{{ form.as_ul }}`, and tests for text it expects; it fails here because
        #   Jinja expects parentheses for method calls (e.g. `{{ form.as_ul() }}`)
        # there's no good way that I can see to intelligently test for this, so I'm
        #   simply going to brute force my way to the result Django expects
        if self._in_test_mode and template == 'registration/login.html':
            t = JinjaTemplate('{{ form.as_ul() }}')
            t.name = 'registration/login.html'
            return t

        # sanity check: if I got an explicit Django template object,
        # I don't want to do anything at all
        if isinstance(template, DjangoTemplate):
            return template

        # sanity check: what if I get a **Jinja** template?
        # don't do anything to that either
        if isinstance(template, JinjaTemplate):
            return template

        # okay, if I have a string or iterable, then figure out the right template
        # and return it
        if isinstance(template, basestring):
            return self._environment.get_template(template)
        elif isinstance(template, (list, tuple)):
            return self._environment.select_template(template)

        # something is wrong; stop
        raise TypeError, 'Unrecognized object sent as a template.'