0

I'm using a layout where a number of javascript libraries are included at the bottom the file, just above the close of the body tag. What I want to do is add some custom javascript below them on various pages but not all pages. Is there some equivalent of

<g:render template="customjs"/>

I can use in the layout gsp that only renders the template if it exists and does not crash on pages without the _customjs.gsp?

Anonymous1
  • 3,877
  • 3
  • 28
  • 42

1 Answers1

0

The answer is here https://stackoverflow.com/a/3394720/1790092
If your controller name is "MyJsController"
Consider wrapping this code into a TagLib for better reuse

<g:set var="controller" value="myJs" />
<g:set var="template" value="customjs" />
<g:set var="patternResolver" value="${new org.springframework.core.io.support.PathMatchingResourcePatternResolver()}" />
<g:set var="pathPattern"  value="grails-app/views/${controller}/_${template}.gsp" />
<g:if test="${patternResolver.getResources(pathPattern)}">
    <g:render template="/${controller}/${template}" />
</g:if><g:else>
    not found!
</g:else>
Community
  • 1
  • 1
Daniel Ribeiro
  • 162
  • 1
  • 9