I've got the line:
static layout = "loggedIn"
in one controller, and the loggedIn.gsp layout is used for views in this controller. I included the same line in another controller, but it doesn't include this layout, rather using main.gsp in the layouts folder. Any thoughts on how I can resolve this / figure out what is going on? Note loggedIn.gsp is in the layouts folder, it is just ignored by the second controller for some reason, which includes the exact same
static layout = "loggedIn"
line
I'd guess that the view has a meta.layout property in it.
Layouts get triggered in this order of precedence:
meta.layout <meta name="layout" content="main"/>
static 'layout' property on the controller
controller/action conventions:
/layouts/${controller}/${action}.gsp
/layouts/${controller}.gsp
configured grails.sitemesh.default.layout property
/layouts/application.gsp
http://www.slideshare.net/laelfrog/grails-layouts-sitemesh (slide 19)
It turns out I had a remnant:
<meta name="layout" content="main" />
in pages for the second controller, which overrides the static layout setting inside the controller.
Related
If you have a controller action, but don't want the view to be rendered with the default layout (in my case layout/main.gsp), is there a render option you can enter into the controller action or something similar?
def comingSoon {
static layout = none; //not correct, but something like this?
}
In your view file, you might see something like this
<html>
<head>
<meta name="layout" content="main"/> // delete this line
<title>Coming Soon</title>
</head>
...
Remove the meta tag with the name="layout". This meta tag is the one that tells sitemesh to use the main layout
Without more details it's difficult to say 100% what case you are in so I'll attempt to answer both.
If you are using dynamically scaffolded views then you'll need to generate the GSPs so you can remove the <meta name="layout" tag from them. This will keep any layout from being used.
Alternatively you could alter the scaffolding templates (within the scaffolding plugin) to include some additional logic about not applying a layout when the domain class has some static property (as your question contains).
You can remove <meta name="layout" content="main"/> to vanish exists layout. Or you may customize layout by edit that main.gsp page in your-project\grails-app\views\layouts\
Generally Rails main application views/layout omits the <html> tag, focusing on defining what should be inside of it, like the <head> and <body> tags.
What I'm actually trying to do is to define a simple attribute inside of the <html> top level tag, but I don't know where to write it.
Any tips?
It's available in application.html.erb in the views/layout folder
This file in included by default in all your views.
In a Grails 1.3.1 app, I want 404 errors to cause the render the main index action of the "list" controller. This controller uses a conventional layout file in the layouts directory (views/layouts/list.gsp), which then uses the included snippet for that action (views/list/index.gsp).
When this action is accessed normally (http://localhost/list/index), then both the layout file and the snippet are applied, and it looks correct. However, when accessed via a 404 rule in the UrlMapping class -- "404"(controller: "list", action: "index") -- the layout file isn't used, and only the snippet is displayed.
Does anyone know why this happens, and if there's any way to get the conventional layout to display for a 404 (or other) error mapping?
I know a while back this was a bug in the version of SiteMesh Grails was using. There is a work around where you can wrap your error pages in:
<g:applyLayout name="main">
</g:applyLayout>
Instead of using the usual:
<meta name="layout" content="main" />
Another thing to look for is the sitemesh.xml configuration file. Sitemesh is turned on/off depending on the content-type of the response, and this file declares the content-types values that site-mesh will process. In my case, an entry for text/html;charset=UTF-8 was not enough for responses with type text/html to be processed.
Are you sure the layout isn't applied? I'm using Grails 1.3.2 and I thought the layout wasn't applied, however it actually was the lack of a model and security tags in the layout after a 404.
If your layout content is derived from such things being available, try a "Hello world" first to see if it shows up.
I would like to create in my master page a control that depending on the rendered viewpage will change a css file
I was thinking to make a specific controller "CssController" and call this CssController from the src attribute of the css link. This controller will be in charge of choosing the right css and sending it back with text/css header.
question: will this approach break the mvc pattern? Indeed the choose of the design would be in charge of a controller. Is there a better alternative?
What i did in my master page was create a contentplaceholder in the <head> section, as so:
<head runat="server">
<title>Title Here</title>
<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>" type="text/javascript" ></script>
<link href="<%= Url.Content("~/Content/css/site.css") %>" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
then, when you create a view using the master page, you can add the view-specific css you want into that content place holder. That way, it'll put your site.css first, plus the desired css, javascript, <script> blocks, whatever, on that particular view.
If you are using separate css files for separate themes, then those would be in fact the view and controller would work. So it does not seem to violate the pattern. But, it does seem to be a more complicated solution than others.
The common way I have seen to do this is with a Jquery Style Switcher
http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/
With a little bit of code you can determine the default style to show on each page
The basic MVC example sets Page Title via the controller. I'm not sure this is any different. If you were using it (for instance) to have users be able to select different skins for their experience, I'm not sure there is any other way to do it, regardless of whether or not it violates the pattern.
I've been going through the book "The Definitive Guide to Grails" (Rocher/Brown) and in Chapter 04, this mysterious thing called a "layout" just appeared with no explanation. (And there's no "layout" in the index. As far as I know, it's never explained.)
How does the system know to "inherit" the pages from layout/main.gsp? There's nothing about "layouts" in the index, and it seems to have just appeared.
On their sample app, a simple store site, the URL mappings for the / homepage say
"/"(controller:"store")
and store controller's empty "index" closure
package com.g2one.gtunes
class StoreController {
def index = {
}
}
simply tells it to render store/index.gsp
The store/index.gsp just has a few lines of HTML; no layout gets included with any directive
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="layout" content="main">
<title>gTunes Store</title>
<g:javascript library="prototype"></g:javascript>
</head>
<body id="body">
<h1>Your online music store and storage service!</h1>
<p>Manage your own library, browse music and purchase new tracks as they become available</p>
</body>
</html>
When I run the sample the page shown for "/" isn't just this simple HTML, it's the contents of "layouts/main.gsp" with this information magically inserted inside it.
I don't see how the information in layout/main.gsp gets applied to the page, how the elements get mixed together. I've been following through the book page by page and this functionality just "appeared" with no explanation.
The <meta name="layout" content="main"> tag includes the layout in the gsp page.
You can view the grails-app/views/layouts/main.gsp to view and modify the layout. You can copy main.gsp to mymain.gsp, modify it, then change layout entry in the gsp page to reference mymain.gsp instead of main.gsp and experiment with customizing your layout preserving your ability to easily back out your changes.
Grails uses sitemesh under the covers (like it uses hibernate and spring) to do view layouts. There is a web-app/WEB-INF/sitemesh.xml configuration file in the project directory as well. This particular file isn't that helpful, but it references a class in the groovy project if you wanted to deeply understand how grails is using sitemesh (this probably isn't necessary).
Here's your directive:
<meta name="layout" content="main">
main.gsp contains <g:layoutHead> and <g:layoutBody>, where the <head> and <body> content of the index.gsp are folded into the layout to create the final page.
One recent trick that appears to work, if you name your layout to match your controller it appears (in Grails 2.3.4 at least) to automatically use that template.
For example, my controller:
// grails-app/controllers/myapp/HomeController.groovy
package myapp
class HomeController {
def index() {
[ myvar: "Test" ]
}
}
my layout:
// grails-app/views/layouts/home.gsp
<html>
<head></head>
<body>
<h1>Home</h1>
<g:layoutBody />
</body>
</html>
my view:
// grails-app/views/home/index.gsp
<p>${ myvar }</p>
renders using the home layout.
Also, you can specify a layout for all your actions in a controller like this:
class HomeController {
static layout = "someotherlayout"
// actions will render using grails-app/views/layouts/someotherlayout.gsp
}