On 03/26/2012 03:36 AM, Nikhil G. Daddikar wrote:
Hello,
I was looking at Velocity syntax and have a few questions.
What is use of parenthesis in the #set directive: #set ($x = 1) Why not
simply: #set $x = 1 ?
Another question is regarding curly braces: What is the use of curly
braces in the directives? E.g. #{set} ($x = 1)
I am sure there are good reasons, just would like to know why.
Thanks.
Because in Velocity anything that's not correctly identified as part of
the syntax is printed literally. I often find myself writing stuff like:
<text class="#if ($foreach.index % $parameters.labelStep ==
0)major-#{else}minor-#{end}label">
Here I have to use the formal notation to separate #end from the
following "label" text. It's not right to say that whenever #end is
found stop parsing further and say that this is the #end keyword, since
this would prevent me from defining my custom macros called #endTable,
for example.
Now it's true that I can't think of any valid usecase when #{set} is
needed for practical reasons, but for consistency it's better to say
that all velocity keywords can be written in the formal notation, not
just those that might need it from time to time.
The same is true for variables, not just keywords.
Going back to my initial statement, the parenthesis help the parser
separate the velocity statements from the plain text. Probably at all
times #set instructions can go on their own line, so we could infere
that it's very safe to say that the #set instruction always ends at the
end of the line. But as you can see from the above example, that's not
true for most of the other keywords, especially when dealing with
space-sensitive output formats. And again for consistency it helps to
have one rule for all the keywords, so that users don't have to remember
which ones require () and which don't.
In summary, some keywords in some cases do need the formal notation to
separate the velocity syntax from the plain text that's going to be
printed. For the same reason, most keywords also require parenthesis
around their argument. For consistency and a more thorough yet simple
definition of the syntax, all keywords follow these rules.
--
Sergiu Dumitriu
http://purl.org/net/sergiu/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org
For additional commands, e-mail: user-h...@velocity.apache.org