4 Using resources - Reference Documentation
Authors: Marc Palmer (marc@grailsrocks.com), Luke Daley (ld@ldaley.com), Peter N. Steinmetz (ndoc3@steinmetz.org)
Version: 1.2.14
Table of Contents
4 Using resources
Now that you know how to declare resources, you need to use them in your page.There are several tags for this purpose, but the primary means is to use the require tag to indicate which modules you need, and the layoutResources tag to perform the rendering of the resources.The require tag causes the framework to look up all the resources required to satisfy your module dependencies. However nothing is rendered at that point.The layoutResources tag is called to render the resources themselves (and internally it calls into external tag for each resource). This tag has special behaviour, in that the first time you call it, it automatically renders only resources with disposition "head". The second time you call it, it automatically renders only resources with disposition "defer".4.1 Linking to CSS, JavaScript etc.
So you need to add two calls to layoutResources tag to your GSP page or sitemesh layout. Normally you will place it in your sitemesh layout:Your grails-app/views/layouts/main.gsp:<html> <head> <g:layoutTitle/> <r:layoutResources/> </head> <body> <g:layoutBody/> <r:layoutResources/> </body> </html>
<html> <head> <meta name="layout" content="main"/> <r:require modules="jquery-ui, blueprint"/> <g:if test="${customerBranding}"> <r:require module="theme_${customer.theme}"/> </g:if> </head> <body> <div> Hello World </div> </body> </html>
4.2 Linking to images
When you need to render an <img> tag that you wish to be subject to the Resources processing chain (e.g. to make it eternally cacheable) you should use the <r:img> tag:<r:img uri="images/logo.png" width="100" height="50"/><r:img dir="images" file="logo.png" width="100" height="50"/>
modules = { images { resource url:'images/logo.png', attrs:[width:100, height:50, alt:'Our logo'], disposition:'inline' resource url:'images/icon/add.png', attrs:[width:32, height:32, alt:'Add'], disposition:'inline' } }
4.3 Linking to resources explicitly, bypassing modules
Sometimes you may need to link to a specific resource, or produce a URL pointing to the specific resource without rendering links to all the modules it depends on - or outside of a context where you can call <r:layoutResources/>. You may even wish to link to an undeclared resource, but still want it to be subject to processing on the fly.To link to CSS or other resources that are not declared in a module you use <r:external>:<r:external uri="js/custom.js"/> <script type="text/javascript"> var urlOfCSSToLoadInJSCode = '${r.external(uri:"css/custom.css").encodeAsJavaScript()}'; </script> <r:external uri="icons/favicon.ico"/>
4.4 Including pieces of JavaScript code generated at runtime
Often in an application, especially those using custom Grails tags and rich UIs, you will need to render fragments of JavaScript code while the page is being rendered. It is not always possible to know what JS code there will be in the page until the render process is finished.The script tag allows you to specify sections of JavaScript text during page rendering, but if using Sitemesh layouts, you will be able to have these fragments appear either in <head> or deferred to the end of the page, just like other JavaScript resources.This integrates with the disposition mechanism, allowing you to throw your JavaScript into a specific location:<r:script> window.alert('This is the end of the page!'); </r:script><r:script disposition='head'> window.alert('This is the head of the page!'); </r:script>
class MyCustomTagLib { def datePicker = { out << r.script(disposition:'head') { out << '$("#'+attrs.id.encodeAsJavaScript()+').datePicker();' } } }