From 15a6869426415d8536a39d4c8053d12f5acdc807 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Mon, 23 Sep 2024 15:55:45 -0400 Subject: [PATCH 1/3] Allow optional attribute for g.render that will ignore if tag if template does not exist. --- .../groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy | 1 + .../org/grails/web/gsp/GroovyPagesTemplateRenderer.java | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy b/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy index 4d8b36cfd6..6988a18e80 100644 --- a/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy +++ b/grails-web-gsp-taglib/src/main/groovy/org/grails/plugins/web/taglib/RenderTagLib.groovy @@ -47,6 +47,7 @@ class RenderTagLib implements TagLibrary { * <g:render template="atemplate" bean="${user}" />
* * @attr template REQUIRED The name of the template to apply + * @attr optional if true, this tag will be ignored when the template does not exist. * @attr contextPath the context path to use (relative to the application context path). Defaults to "" or path to the plugin for a plugin view or template. * @attr bean The bean to apply the template against * @attr model The model to apply the template against as a java.util.Map diff --git a/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java b/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java index 3fa2a6ec11..2222217016 100644 --- a/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java +++ b/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java @@ -115,6 +115,10 @@ public void render(GrailsWebRequest webRequest, TemplateVariableBinding pageScop final Object controller = webRequest.getAttribute(GrailsApplicationAttributes.CONTROLLER, GrailsWebRequest.SCOPE_REQUEST); Template t = findAndCacheTemplate(controller, pageScope, templateName, contextPath, pluginName, uri); if (t == null) { + String optional = getStringValue(attrs, "optional"); + if (Boolean.parseBoolean(optional.isBlank()? "false" : optional)) { + return; // allow missing templates if optional == "true" + } throw new GrailsTagException("Template not found for name [" + templateName + "] and path [" + uri + "]"); } From 2104f0900e60a1bbdda10380720247f8fa1ffaa0 Mon Sep 17 00:00:00 2001 From: James Fredley Date: Mon, 23 Sep 2024 17:04:25 -0400 Subject: [PATCH 2/3] Update gradle.yml --refresh-dependencies --- .github/workflows/gradle.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index d7c1f45e57..9e53660c8d 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -28,7 +28,7 @@ jobs: env: GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }} with: - arguments: build + arguments: build --refresh-dependencies - name: Upload Distribution if: success() && matrix.java == '11' uses: actions/upload-artifact@v4 @@ -85,4 +85,4 @@ jobs: BRANCH: gh-pages FOLDER: build/docs COMMIT_EMAIL: behlp@unityfoundation.io - COMMIT_NAME: Puneet Behl \ No newline at end of file + COMMIT_NAME: Puneet Behl From f8e5f036954e3e8b2d348f7e103f4205eee194b6 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Tue, 24 Sep 2024 10:09:41 -0500 Subject: [PATCH 3/3] Place boolean conversion in separate method --- .../org/grails/web/gsp/GroovyPagesTemplateRenderer.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java b/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java index 2222217016..b2f6d2b4da 100644 --- a/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java +++ b/grails-web-gsp/src/main/groovy/org/grails/web/gsp/GroovyPagesTemplateRenderer.java @@ -115,8 +115,7 @@ public void render(GrailsWebRequest webRequest, TemplateVariableBinding pageScop final Object controller = webRequest.getAttribute(GrailsApplicationAttributes.CONTROLLER, GrailsWebRequest.SCOPE_REQUEST); Template t = findAndCacheTemplate(controller, pageScope, templateName, contextPath, pluginName, uri); if (t == null) { - String optional = getStringValue(attrs, "optional"); - if (Boolean.parseBoolean(optional.isBlank()? "false" : optional)) { + if (getBooleanValue(attrs, "optional")) { return; // allow missing templates if optional == "true" } throw new GrailsTagException("Template not found for name [" + templateName + "] and path [" + uri + "]"); @@ -293,6 +292,12 @@ private String getStringValue(Map attrs, String key) { return String.valueOf(val); } + private boolean getBooleanValue(Map attrs, String key) { + String val = getStringValue(attrs, key); + if (val.isBlank()) return false; + return Boolean.parseBoolean(val); + } + public void setGroovyPageLocator(GrailsConventionGroovyPageLocator locator) { groovyPageLocator = locator; }