diff --git a/grails-app/controllers/PdfTestController.groovy b/grails-app/controllers/PdfTestController.groovy index 0ee8e70..2c72875 100644 --- a/grails-app/controllers/PdfTestController.groovy +++ b/grails-app/controllers/PdfTestController.groovy @@ -17,6 +17,6 @@ class PdfTestController { } protected getTemplate() { - [template: 'pdf', model: [var: params.id]] + [template: '/pdf', model: [var: params.id]] } } \ No newline at end of file diff --git a/grails-app/services/grails/plugin/pdf/PdfRenderingService.groovy b/grails-app/services/grails/plugin/pdf/PdfRenderingService.groovy index 156657e..2f2c37c 100644 --- a/grails-app/services/grails/plugin/pdf/PdfRenderingService.groovy +++ b/grails-app/services/grails/plugin/pdf/PdfRenderingService.groovy @@ -31,6 +31,7 @@ class PdfRenderingService { static DEFAULT_BUFFERED_IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB def groovyPagesTemplateEngine + def groovyPagesUriService OutputStream render(Map args, OutputStream outputStream = new ByteArrayOutputStream()) { def doc = generateDoc(args) @@ -247,35 +248,49 @@ class PdfRenderingService { } protected createTemplate(args) { - groovyPagesTemplateEngine.createTemplate(resolveGspTemplateResource(args)) - } - - protected resolveGspTemplateResource(Map args) { assertTemplateArgumentProvided(args) + def templateName = args.template - def resource = groovyPagesTemplateEngine.getResourceForUri('_' + args.template) - if (!resource || !resource.exists()) { - if (args.plugin) { - def plugin = PluginManagerHolder.pluginManager.getGrailsPlugin(args.plugin) - if (!plugin) { - throw new IllegalArgumentException("No plugin named '$args.plugin' is installed") - } - def pathToTemplate = '/plugins/'+GCU.getScriptName(plugin.name)+'-'+plugin.version+'/'+GrailsResourceUtils.GRAILS_APP_DIR+'/views/' - def uri = GrailsResourceUtils.WEB_INF +pathToTemplate + args.template + ".gsp" - resource = groovyPagesTemplateEngine.getResourceForUri(uri) + if (templateName.startsWith("/")) { + if (!args.controller) { + args.controller = "" + } + } else { + if (!args.controller) { + throw new IllegalArgumentException("template names must start with '/' if controller is not provided") } } - if (!resource || !resource.exists()) { + def contextPath = getContextPath(args) + def controllerName = args.controller instanceof CharSequence ? args.controller : groovyPagesUriService.getLogicalControllerName(args.controller) + def templateUri = groovyPagesUriService.getTemplateURI(controllerName, templateName) + def uris = ["$contextPath/$templateUri", "$contextPath/grails-app/views/$templateUri"] as String[] + def template = groovyPagesTemplateEngine.createTemplateForUri(uris) + + if (!template) { throwUnknownTemplateError(args) } - resource + template + } + + protected getContextPath(args) { + def contextPath = args.contextPath?.toString() ?: "" + def pluginName = args.plugin + + if (pluginName) { + def plugin = PluginManagerHolder.pluginManager.getGrailsPlugin(pluginName) + if (plugin && !plugin.isBasePlugin()) { + contextPath = plugin.pluginPath + } + } + + contextPath } protected assertTemplateArgumentProvided(Map args) { if (!args.template) { - throw new IllegalArgumentException("The 'templatew' argument must be specified") + throw new IllegalArgumentException("The 'template' argument must be specified") } } diff --git a/grails-app/views/bad-xml.gsp b/grails-app/views/_bad-xml.gsp similarity index 100% rename from grails-app/views/bad-xml.gsp rename to grails-app/views/_bad-xml.gsp diff --git a/grails-app/views/pdf.gsp b/grails-app/views/_pdf.gsp similarity index 100% rename from grails-app/views/pdf.gsp rename to grails-app/views/_pdf.gsp diff --git a/plugins/pdf-plugin-test/grails-app/views/plugin-pdf.gsp b/plugins/pdf-plugin-test/grails-app/views/_plugin-pdf.gsp similarity index 100% rename from plugins/pdf-plugin-test/grails-app/views/plugin-pdf.gsp rename to plugins/pdf-plugin-test/grails-app/views/_plugin-pdf.gsp diff --git a/test/integration/grails/plugin/pdf/GrailsPdfSpec.groovy b/test/integration/grails/plugin/pdf/GrailsPdfSpec.groovy index 8fda404..671041a 100644 --- a/test/integration/grails/plugin/pdf/GrailsPdfSpec.groovy +++ b/test/integration/grails/plugin/pdf/GrailsPdfSpec.groovy @@ -43,7 +43,7 @@ class GrailsPdfSpec extends IntegrationSpec { def renderWithUnknownTemplateThrowsException() { when: - pdfRenderingService.render(template: "asdfasdfasd") + pdfRenderingService.render(template: "/asdfasdfasd") then: thrown(UnknownTemplateException) } @@ -177,11 +177,11 @@ class GrailsPdfSpec extends IntegrationSpec { } protected getSimpleTemplate(Map args = [:]) { - [template: 'pdf', model: [var: 1]] + args + [template: '/pdf', model: [var: 1]] + args } protected getPluginTemplate(Map args = [:]) { - [template: 'plugin-pdf', plugin: 'pdf-plugin-test', model: [var: 1]] + args + [template: '/plugin-pdf', plugin: 'pdf-plugin-test', model: [var: 1]] + args } }