grailsgspsitemesh

What is is causing the script block in the html head of a gsp to be overwritten


I have 2 grails views that both refer to the same layout:

index.gsp & custom.gsp

Both pages have a <script> block in the <head>. I would like to be able to go to the custom.gsp directly OR work with the content in a tab in index.gsp.

I'm including the custom.gsp content using:

<g:include controller='controllername' action='custom'/>

It appears as if the custom.gsp's <script> block is overwritting the index.gsp's <script> block. If I remove the include everything else on the index page operates normally. If I add the include the code in the <script> block of index.gsp is not executed and upon inspection does not appear to be there.

What is really happening here and why? How can I structure things differently so this doesn't happeN?

UPDATE

I've tried moving the code in custom.gsp out of a <script> block and into a separate javascript file. However; when the include happens now it still pulls the code in as a <script> block and replaces the old one.

I've tried moving the block of each one into the body instead and I've tried adding id attributes to each thinking maybe sitemesh might see it as a unique block then. Still no luck.

UPDATE #2

I tried adding this to the body of index.gsp:

<div>this is a script
    <script type="application/javascript">
        var xyz='xyz';
    </script>
</div>

And the text this is a script is visible but the script is gone!

UPDATE #3

update #2 is wrong, the text is visible but the script is actually there too. I thought it was gone because I was observing the same behavior as when it was gone. However; the real issue is that the entire head of index.gsp was replaced with the included page's head so index.gsp lost the libraries it needed to execute the script code.


Solution

  • As commented by @joshua the view returned by this call:

    <g:include controller='controllername' action='custom'/>
    

    must be a fragment instead of a whole gsp view with html, head .. tags.

    Think of this when including views using g:include, if you have HomeController and NewsController (check figures below), and you want to include NewsController's view with HomeController's view,

    enter image description here

    You have to define an action on your NewsController that will only return the content fragment you see on the figure. To do this you can keep the news.gsp view but refactor it in such a way you put the content section in a new template(content fragment). I hope you get what I mean, but leave a comment if something is not clear on my example.