简体   繁体   中英

Space in grails GSP

Sorry grails / gsp noob here. I am trying to fix a bug where a space is always inserted before some text.

The text is just displayed based on a else if:

<g:elseif test="${index > 0 && merch <= transaction.Transactions.size() - 2}">
, ${trans.name}
</g:elseif>

A space is always displayed before the comman. Why? How do I get rid of the space?

Why?

Because there's white space (the newline) between the opening elseif tag and the comma.

How do I get rid of the space?

Put the comma directly after the opening tag rather than on the next line. You can achieve this by moving the newline inside the tag rather than after it:

<g:elseif test="${index > 0 && merch <= transaction.Transactions.size() - 2}"
  >, ${trans.name}</g:elseif>

or by using a comment

<g:elseif test="${index > 0 && merch <= transaction.Transactions.size() - 2}"><%--
  --%>, ${trans.name}<%--
--%></g:elseif>

You may find you need to use a similar trick to squash the space between the </g:if> and the <g:elseif> tags, and immediately before the <g:if> . If this all starts to look a bit unwieldy you may prefer to use Groovy code rather than GSP tags for the conditionals

stuff before<%
if(something) {
  %>some content<%
} else if(index > 0 && merch <= transaction.Transactions.size() - 2) {
  %>, ${trans.name}<%
} else {
  %>something else<%
}
%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM