I have a page that uses quite a lot of include files. It dynamically selects the correct include file to use. I’d like to (in the include file) be able to specify to grails that I’d like it to include a specific <link rel="stylesheet"/> tag into the head, on the fly.
Something output like this..
<html>
<head>
<title>My Life :: My Pets</title>
<link rel="stylesheet" href="style.css" type="text/css"> <!-- Normal Site Style -->
<link rel="stylesheet" href="include-my-pets" type="text/css"> <!-- Dynamic Style for Include -->
</head>
<body>
<h1>My Pets</h1>
<!-- This is the include file start -->
In the GSP here I said something like:
<r:require disposition="head">
<link rel="stylesheet" href="include-my-pets" type="text/css"> <!-- Dynamic Style -->
</r:require>
to get the CSS link tag pushed into the head.
<!-- This is the include file end -->
</body>
</html>
This has worked very well for me using the <r:script/> tag. I can specify anywhere in any include file:
<r:script disposition="head">
alert ('hello')
</r:script>
And my layout automatically gets the alert hello stuck inside a <script/> tag in the head of the page. It is also removed from the body.
This is not the “grails” way of doing things, but this is how I implemented it. Part of the reason I did this is because I feel that defining resources in ApplicationResources.groovy may not be obvious to non-grails developers that are not familiar with the resources plugin. I preferred the idea of having explicit resource links in the include files which are obvious.
The way I’ve done it now is such.
Kitty and Doggy needed custom CSS and JavaScript (they’re both part of pets), but the other links, such as MyLife don’t need them.
In URLMappings.groovy
layout.gsp
In pets.gsp