Skip to content

Commit

Permalink
Merge pull request #495 from codeconsole/6.2.x-optional-templates
Browse files Browse the repository at this point in the history
Allow optional attribute for g.render that will ignore if tag if template does not exist.
  • Loading branch information
codeconsole authored Sep 24, 2024
2 parents 2104f09 + f8e5f03 commit 96c2733
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class RenderTagLib implements TagLibrary {
* &lt;g:render template="atemplate" bean="${user}" /&gt;<br/>
*
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ 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) {
if (getBooleanValue(attrs, "optional")) {
return; // allow missing templates if optional == "true"
}
throw new GrailsTagException("Template not found for name [" + templateName + "] and path [" + uri + "]");
}

Expand Down Expand Up @@ -289,6 +292,12 @@ private String getStringValue(Map<String, Object> attrs, String key) {
return String.valueOf(val);
}

private boolean getBooleanValue(Map<String, Object> attrs, String key) {
String val = getStringValue(attrs, key);
if (val.isBlank()) return false;
return Boolean.parseBoolean(val);
}

public void setGroovyPageLocator(GrailsConventionGroovyPageLocator locator) {
groovyPageLocator = locator;
}
Expand Down

0 comments on commit 96c2733

Please sign in to comment.