Dollar and exclamation mark (bang) symbols in VTL
Asked Answered
F

2

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?

Fluter answered 31/8, 2018 at 8:56 Comment(0)
M
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.

Manteltree answered 4/9, 2018 at 10:2 Comment(0)
F
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.

Fluter answered 31/8, 2018 at 8:56 Comment(1)
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 ! symbolFluter

© 2022 - 2024 — McMap. All rights reserved.