如何在视图中声明javascript资产,以便使用Grails资产管道在页脚内呈现

如何在视图中声明javascript资产,以便使用Grails资产管道在页脚内呈现,grails,grails-plugin,Grails,Grails Plugin,由于没有延迟选项,因此: <asset:javascript src="custom_view_script.js"/> 除了资源插件之外,还有什么可以用来将特定于视图的脚本放在结束正文标记之前,而不在布局中全局声明它 我确实知道: <asset:deferredScripts/> 但这只处理页面脚本,不包括。最简单的方法是使用站点网格 在您的布局中,您需要 <g:pageProperty name="page.script"/> 现在,您可以在

由于没有延迟选项,因此:

<asset:javascript src="custom_view_script.js"/>

除了资源插件之外,还有什么可以用来将特定于视图的脚本放在结束正文标记之前,而不在布局中全局声明它

我确实知道:

<asset:deferredScripts/>


但这只处理页面脚本,不包括。最简单的方法是使用站点网格

在您的布局中,您需要

<g:pageProperty name="page.script"/>
现在,您可以在布局中保留g:pageProperty,但您可以在页面中这样做:

<mycontent:addContent tag="script">
<script type="application/javascript">
... your code here ...
</script>
</mycontent:addContent>

... 你的代码在这里。。。

这将收集您放入不同视图和模板中的所有内容,然后将其显示在最终的html中,即g:pageProperty标记所在的位置。

这是使用sitemash的一个很好的解决方案。一、 然而,试图找到一种使用Grails资产插件的方法。我自己也尝试过使用资产插件,但没有效果,因为你说的是“资源插件之外”。。。
class MyContentTagLib implements RequestConstants {

    static namespace = "mycontent"

    Closure addContent = { Map attrs, body ->
        if( body != null ) {
            def htmlPage = getPage()
            if( htmlPage instanceof GSPSitemeshPage && attrs.tag ) {
                def name = attrs.tag
                def sitemeshPage = (GSPSitemeshPage) htmlPage
                StreamCharBuffer currentContent = sitemeshPage.getContentBuffer( "page.$name" ) as StreamCharBuffer
                StreamCharBuffer newContent = wrapContentInBuffer( body )
                if( currentContent ) {
                    newContent.writeTo( currentContent.writer )
                    newContent = currentContent
                }
                sitemeshPage.setContentBuffer( name, newContent )
            }
        }
    }

    private AbstractHTMLPage getPage() {
        return (AbstractHTMLPage)getRequest().getAttribute(PAGE)
    }

    private StreamCharBuffer wrapContentInBuffer(Object content) {
        if (content instanceof Closure) {
            content = content()
        }
        if (!(content instanceof StreamCharBuffer)) {
            // the body closure might be a string constant, so wrap it in a StreamCharBuffer in that case
            FastStringWriter stringWriter=new FastStringWriter()
            stringWriter.print((Object)content)
            StreamCharBuffer newbuffer = stringWriter.buffer
            newbuffer.setPreferSubChunkWhenWritingToOtherBuffer(true)
            return newbuffer
        } else {
            return (StreamCharBuffer)content
        }
    }

}
<mycontent:addContent tag="script">
<script type="application/javascript">
... your code here ...
</script>
</mycontent:addContent>