Search code examples
grailssitemesh

How to include css and javascript to main template head section using the grails web framework


I have been using the grails UI performance plugin which handles this with the p:dependantJavascript taglibs.

So say im working on a template _someTemplate.gsp and this template only has the following content:

<div onclick="doSomething()">someContent</div>

now i would like to add the javascript doSomething() to the head section of the page whenever this template is loaded. Is ther any grails magic to do this?


Solution

  • You can put javascript tag in the template, like:

    <g:javascript src="file.js"/>
    

    UPDATE: The general way to include any javascript & CSS file into the head of a layout is to create a full template:

    <html>
        <head>
            <title><g:layoutTitle default="An example decorator" /></title>
            <!-- javascript & css go here, just like normal gsp/html page -->
            <g:layoutHead /> <!-- for the specific page -->
        </head>
        <body>
    
            <g:layoutBody /> <!-- render the specific page body -->
    
            <div onclick="doSomething()">someContent</div>
        </body>
    </html>
    

    More detailed instructions can be found here.