Velocity
  1. Velocity
  2. VELOCITY-756

New notation for replacing null values with a specified string

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Engine
    • Labels:
      None

      Description

      If I have a variable $foo and it is null, I have the option of using the silent notation $!foo for not writing it. But often when genrating HTML pages I want to express that the values is missing, e.g. by a "-". That means that I have to write my code like this:

      #if($foo)
      $foo
      #else
      -
      #end

      Wouldn't it be great if there were a new option like $?foo (or whatever chararter would be suitable instead of "?") that would print the variable $foo if it was not null and otherwise print a predefined character like "-".

      /Ludwig

        Activity

        Ludwig Magnusson created issue -
        Hide
        Nathan Bubna added a comment -

        As an alternate to #if #else, there is also:

        http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DisplayTool.html

        which provides $display.alt($foo) and $display.alt($foo, 'n/a') and many other such display control methods. This is ideally where "extra" features like this go. The tool approach has several advantages:

        • keeps VTL syntax simple and contained (feature creep has its costs)
        • provides API that is easy to extend and enhance
        • typically provides much more clear and user-friendly API

        Unless the $?foo syntax were to become something frequently requested (which it has never been), i don't see us working to adopt it anytime soon, for the reason of keeping things simple.

        Show
        Nathan Bubna added a comment - As an alternate to #if #else, there is also: http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DisplayTool.html which provides $display.alt($foo) and $display.alt($foo, 'n/a') and many other such display control methods. This is ideally where "extra" features like this go. The tool approach has several advantages: keeps VTL syntax simple and contained (feature creep has its costs) provides API that is easy to extend and enhance typically provides much more clear and user-friendly API Unless the $?foo syntax were to become something frequently requested (which it has never been), i don't see us working to adopt it anytime soon, for the reason of keeping things simple.
        Hide
        Ludwig Magnusson added a comment -

        I did not know that this feature existed and I agree that it is a better and more flexible solution.
        Thanks for the comment.

        Show
        Ludwig Magnusson added a comment - I did not know that this feature existed and I agree that it is a better and more flexible solution. Thanks for the comment.
        Hide
        Jarkko Viinamäki added a comment -

        Closing based on Nathan's comment.

        Show
        Jarkko Viinamäki added a comment - Closing based on Nathan's comment.
        Jarkko Viinamäki made changes -
        Field Original Value New Value
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Won't Fix [ 2 ]
        Mark Thomas made changes -
        Workflow jira [ 12501134 ] Default workflow, editable Closed status [ 12551722 ]
        Mark Thomas made changes -
        Workflow Default workflow, editable Closed status [ 12551722 ] jira [ 12552172 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Ludwig Magnusson
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development