groovy
  1. groovy
  2. GROOVY-287

$!{text} toString()'s to "" instead of null

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.5
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      For GroovyTemplates its not very desirable to have alot of "null" printed everywhere we might have included a possible null value. So it would be nice to have a way to make GString print "" instead of null.

      Velocity does that via $!

      {text}

      which I think is ok, but we may just parse the Template text and always "insert" $

      {(text == null) ? "" : text"}

      since I think its always the desired behaviour for gstrings to not print null (within Templates).

        Issue Links

          Activity

          Hide
          Yuri Schimke added a comment -

          What about just making it such that GroovyTemplates (possibly optionally) don't print nulls.

          Its very amateurish when websites print nulls all over the place.

          Especially with the null safe method invocations, it would be nice to be able to use.

          $

          {user->lastLogin}

          Another thought, would be to use an OR, since this will let you give an alternative.

          $

          {user->lastLogin || "Never Logged In"}

          But I don't think this will work, since || quite rightly is a boolean expression, even though "if (a)" works.

          1> print (a || "b")
          2> execute
          true

          Show
          Yuri Schimke added a comment - What about just making it such that GroovyTemplates (possibly optionally) don't print nulls. Its very amateurish when websites print nulls all over the place. Especially with the null safe method invocations, it would be nice to be able to use. $ {user->lastLogin} Another thought, would be to use an OR, since this will let you give an alternative. $ {user->lastLogin || "Never Logged In"} But I don't think this will work, since || quite rightly is a boolean expression, even though "if (a)" works. 1> print (a || "b") 2> execute true
          Hide
          james strachan added a comment -

          Maybe use the ?: notation.

          println "Hello there $

          {user ?: "uknown user"}

          how are you doing?"

          Show
          james strachan added a comment - Maybe use the ?: notation. println "Hello there $ {user ?: "uknown user"} how are you doing?"
          Hide
          james strachan added a comment -

          Velocity uses the nice notation that if you don't wanna print nulls then you do

          $!foo

          which I like.

          Also if we wanna handle printing of things with defaults, we should support

          defaultValue = "anonymous"
          println "hello $

          {foo || defaultValue}

          there"

          Using the || operator to allow nulls to be handled in a more graceful way

          Show
          james strachan added a comment - Velocity uses the nice notation that if you don't wanna print nulls then you do $!foo which I like. Also if we wanna handle printing of things with defaults, we should support defaultValue = "anonymous" println "hello $ {foo || defaultValue} there" Using the || operator to allow nulls to be handled in a more graceful way
          Hide
          Paul King added a comment -

          I believe this is now covered by:

          def user
          assert "Hello ${user ?: "unknown user"}, how are you?" == 'Hello unknown user, how are you?'
          user = "Paul"
          assert "Hello ${user ?: "unknown user"}, how are you?" == 'Hello Paul, how are you?'
          

          Please reopen if you believe further enhancement is required.

          Show
          Paul King added a comment - I believe this is now covered by: def user assert "Hello ${user ?: " unknown user "}, how are you?" == 'Hello unknown user, how are you?' user = "Paul" assert "Hello ${user ?: " unknown user "}, how are you?" == 'Hello Paul, how are you?' Please reopen if you believe further enhancement is required.

            People

            • Assignee:
              Paul King
              Reporter:
              Thomas Heller
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: