Search code examples
templatesgrailsgspsitemesh

In a grails gsp template, how can I use a server side comment without sitemesh throwing an error?


When I use a standard jsp comment block in a gsp template

<%-- some server-side comment --%>    

, sitemesh throws an 'unexpected token' error. Is there another comment syntax I can use?


Solution

  • There's a little confusion among previous answers (and the question itself) that I wish was explained to me at first. There are a few types of server side comments on a .gsp. So within the .gsp document Server side comments go as follow:

    <%@ page contentType="text/html; charset=UTF-8" %>
    <html>
    <head></head>
    <body>
        <!-- the basic HTML comment (not on server side) -->
        <h1>Visible on client side</h1>
    
        <%-- GSP common comment (server side only) --%>
        %{-- GSP alternative approach (again, on server side only) --}%
        <g:if test="${true}">
            <h1>Invisible on client side, just in source code</h1>
        </g:if>
    
        <p>and the one asked for happens elsewhere, 
        whenever you write classic Groovy script</p>
        <g:set var="myTitle"/>
        <%
            myVar = 'comment'
            if(myVar.equals('comment')){
                /*Needs the classic Java comment, 
                this happens whether you're writing a simple .gsp 
                or any _template.gsp*/
                myTitle = "<h1>Visible on server side only</h1>".encodeAsRaw()
            }
        %>
        ${myTitle}
    
        <p>.gsp template does not modify comment behaviour</p>
        <g:render template="/templates/myTemplate" model="[:]"/>
    </body>
    </html>
    

    file: _myTemplate.gsp

    <h2>Template</h2>
    
    <!-- visible -->
    <% invisible %>
    %{-- invisible --}%
    <% /*invisible*/ %>
    

    (Grails 2.5.5)