diff --git a/tailbone/views/master.py b/tailbone/views/master.py index 9f7112cd..08f298c9 100644 --- a/tailbone/views/master.py +++ b/tailbone/views/master.py @@ -35,7 +35,7 @@ from edbob.util import prettify import formalchemy from pyramid import httpexceptions -from pyramid.renderers import get_renderer, render_to_response +from pyramid.renderers import get_renderer, render_to_response, render from tailbone import forms from tailbone.views import View @@ -387,6 +387,42 @@ class MasterView(View): return render_to_response('{}/{}.mako'.format(self.get_template_prefix(), template), context, request=self.request) + # TODO: merge this logic with render_to_response() + def render(self, template, data): + """ + Render the given template with the given context data. + """ + context = { + 'master': self, + 'model_title': self.get_model_title(), + 'model_title_plural': self.get_model_title_plural(), + 'route_prefix': self.get_route_prefix(), + 'permission_prefix': self.get_permission_prefix(), + 'index_title': self.get_index_title(), + 'index_url': self.get_index_url(), + 'action_url': self.get_action_url, + } + context.update(data) + + # First try the template path most specific to the view. + try: + return render('{}/{}.mako'.format(self.get_template_prefix(), template), + context, request=self.request) + + except IOError: + + # Failing that, try one or more fallback templates. + for fallback in self.get_fallback_templates(template): + try: + return render(fallback, context, request=self.request) + except IOError: + pass + + # If we made it all the way here, we found no templates at all, in + # which case re-attempt the first and let that error raise on up. + return render('{}/{}.mako'.format(self.get_template_prefix(), template), + context, request=self.request) + def get_fallback_templates(self, template): return ['/master/{}.mako'.format(template)]