Skip to content

Commit

Permalink
Rewrote the GSP resolution to map to what Grails does more closely. I…
Browse files Browse the repository at this point in the history
…t also will now use compiled views if they are available.
  • Loading branch information
ldaley committed Jun 9, 2010
1 parent d165d72 commit 5af889e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion grails-app/controllers/PdfTestController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class PdfTestController {
}

protected getTemplate() {
[template: 'pdf', model: [var: params.id]]
[template: '/pdf', model: [var: params.id]]
}
}
49 changes: 32 additions & 17 deletions grails-app/services/grails/plugin/pdf/PdfRenderingService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
}

Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions test/integration/grails/plugin/pdf/GrailsPdfSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GrailsPdfSpec extends IntegrationSpec {

def renderWithUnknownTemplateThrowsException() {
when:
pdfRenderingService.render(template: "asdfasdfasd")
pdfRenderingService.render(template: "/asdfasdfasd")
then:
thrown(UnknownTemplateException)
}
Expand Down Expand Up @@ -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
}

}

0 comments on commit 5af889e

Please sign in to comment.