开发者

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

&开发者_开发问答lt;%-- some server-side comment --%>    

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


The following works for me

%{-- <div>hello</div> --}%


You are missing a '%' sign. Write it as :

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


The original question was asking how to comment out anything in the GSP file. The only one that worked for me is

<%-- some code to comment out --%>,

the other answers won't work especially if the code being commented are grails tags. %{ and <% didn't work.


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)


A regular java comment block will work

<% /*  some server side comment */ %>


if you are writing a gsp that wants to display a uninterpreted grails g: tag, e.g. you want <g:link ... to appear as-is on the page, without being interpreted server-side, the following worked nicely for me.

In both the start and end tags, replace the < with &lt;

e.g.

<g:link...>...</g:link> gets interpreted server-side and shows up in the page a link.

&lt;g:link ...>...&lt;/g:link ...> shows up in the front-end page as <g:link...>...</g:link>


<%-- server side code --%> should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜