4

I've recently encountered these two variables in some Velocity code:

$!variable1
!$variable2

I was surprised by the similarity of these so I became suspicious about the correctness of the code and become interested in finding the difference between two.

Is it possible that velocity allows any order of these two symbols or do they have different purpose? Do you know the answer?

Jr.
  • 115
  • 2
  • 10

2 Answers2

8

@Jr. Here is the guide I followed when doing VM R&D: http://velocity.apache.org/engine/1.7/user-guide.html

Velocity uses the !$ and $! annotations for different things. If you use !$ it will basically be the same as a normal "!" operator, but the $! is used as a basic check to see if the variable is blank and if so it prints it out as an empty string. If your variable is empty or null and you don't use the $! annotation it will print the actual variable name as a string.

0

I googled and stackoverflowed a lot before I finally found the answer at people.apache.org.

According to that:

It is very easy to confuse the quiet reference notation with the boolean not-Operator. Using the not-Operator, you use !${foo}, while the quiet reference notation is $!{foo}. And yes, you will end up sometimes with !$!{foo}...

Easy after all, shame it didn't struck me immediately. Hope this helps someone.

Jr.
  • 115
  • 2
  • 10
  • It is not easy to search for special characters in Google...I didn't find anything helpful so I opened the documentation and started searching for ! symbol – Jr. Aug 31 '18 at 09:03