0

I am currently upgrading to Grails 4. The use of ternary operators are not recognized in GSP pages. The following code:

     <td>${(user?.active) ? '<i class="icon icon-plus" style="color: green;"></i> <span style="color: green">Active</span>' : '<i class="icon-remove-circle" style="color: red;"></i> <span style="color: red">Inactive</span>'}</td>

prints the string instead of the displaying the fontAwesome icon. If I remove the single quotes around the <i class = "icon... > the following error is produced.

     unexpected token: ? @ line 190, column 275, it) { return (user?.active) ? <i class                                         
                                                                             ^

This was not a problem in the previous Grails version. Can anyone recommend a solution besides turning the line of code into an if-else statement?

Here is a screenshot of what is displayed.

image of output displayed

doelleri
  • 19,232
  • 5
  • 61
  • 65

1 Answers1

2

Rather than the ternary operator not being recognized, you have the problem of HTML being displayed as a string instead of being rendered. This is because GSP expressions (inside ${}) are encoded as HTML by default.

To handle this single situation, wrap your expression in the raw() method:

<td>${raw(user?.active ? '<i class="icon icon-plus" style="color: green;"></i> <span style="color: green">Active</span>' : '<i class="icon-remove-circle" style="color: red;"></i> <span style="color: red">Inactive</span>')}</td>

If you want to change this behavior for the entire page, you can add to the top of the GSP:

<%@page expressionCodec="none" %>

If you want to change this default globally, you can set in application.yml:

grails:
    views:
        gsp:
            codecs:
                expression: none
doelleri
  • 19,232
  • 5
  • 61
  • 65