IGNORE THIS ANSWER, see the comment below by the asset-pipeline creator about how this already exists in the resource pipeline plugin as <asset:script> .
OLD RESPONSE:
Sitemesh makes my head hurt, and as far as I can tell, a solution using the g:layoutHead and g:layoutBody not something that would allow you to put together a bunch of different javascript fragments and emit them all in a single block.
An application that I recently started working on the rather heavily used r:script tag, and will use it several times on the same gsp page (on various included templates).
As I plan to solve this in my application, this is taglib, which just does what the r:script tag did. Collect all the javascript pending snippets and emit them when the user asks them to be emitted.
Here is an example taglib that can do this:
class DeferTagLib { static namespace = 'defer' static defaultEncodeAs = [emitScript: 'raw']
Then on my gsp pages / snippets, I can simply replace any previous calls with r:script with defer:script :
<defer:script> // this is before </defer:script> ... maybe another template that included ... <defer:script> alert("foo!"); </defer:script> ... later <defer:script> // after </defer:script>
And in my layout below, I can just do it
<html> <head> .... shared head stuff .... <g:layoutHead/> </head> <body> .... body stuff before layout <g:layoutBody/> .... other body stuff after layout <defer:emitScript/> </body> </html>
This will create a partition after the body, which looks like
... the actual body from the gsp <script> </script> </body> </html>
source share