You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Nikhil G. Daddikar" <ng...@celoxis.com> on 2012/03/26 09:36:09 UTC

Velocity syntax

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.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Velocity syntax

Posted by Nathan Bubna <nb...@gmail.com>.
On Mon, Mar 26, 2012 at 12:36 AM, Nikhil G. Daddikar <ng...@celoxis.com> 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 ?

Why do you ask? Is there some reason that a space is superior to a
parenthesis?  The latter is easier for my eyes to parse, but Sergiu is
quite right that consistency with other places where parentheses are
more obviously useful is the key reason.

> 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.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Velocity syntax

Posted by Scott Gentry <sg...@gmail.com>.
I say this without authority on the subject, but I'm guessing that the
parenthesis are used to help parse the strings.  As you may or may not have
noticed in the user's
guide<http://velocity.apache.org/engine/devel/user-guide.html#VTL:_Formatting_Issues>,
Velocity is white-space agnostic.  Thus, if the grammar allowed for your
usage pattern, a certain style would have to be set for us, otherwise it
wouldn't be known where the directive ends.

With regards to the curly braces, I will also point you to the user's
guide<http://velocity.apache.org/engine/devel/user-guide.html#formalreferencenotation>
section
on formal reference notation.  I found few cases where we needed to use
formal reference notation in any of our templates, but there are times when
it is worth knowing.

On Mon, Mar 26, 2012 at 3:36 AM, Nikhil G. Daddikar <ng...@celoxis.com> 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.
>
>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: user-unsubscribe@velocity.**apache.org<us...@velocity.apache.org>
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

Re: Velocity syntax

Posted by Sergiu Dumitriu <se...@xwiki.com>.
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-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org