Menu

Using curly bracket with directive

Help
2007-05-03
2013-04-17
  • Ranco Marcus

    Ranco Marcus - 2007-05-03

    I'm experiencing a problem when I use curly brackets around the else directive (see example below). The problem is that the string #{else} is itself copied to the output instead of being recognized and parsed as a directive.

    #if($column.Nullable)NULL#{else}NOT NULL#end

    Can anybody give me a hint on how this feature is used? Or is it a bug perhaps?

    Kind regards,

    Ranco

     
    • Mike Bridge

      Mike Bridge - 2007-05-03

      Hi-

      Yes, I think that is supposed to work---it seems like a bug.  You may need to use a workaround, like using two #if statements, or else define an empty string as a variable:

        #set($empty="")
        #if($column.Nullable)NULL#else${empty}NOT NULL#end

      Cheers,

      -Mike

       
      • Ranco Marcus

        Ranco Marcus - 2007-05-03

        Mike, thanks for your response. The workaround works perfectly.

        Btw, why are the curly braces needed around empty?

        Regards,

        Ranco

         
        • Mike Bridge

          Mike Bridge - 2007-05-03

          Hi-

          Normally, you can put curly braces around variables and directives to set them off from the surrounding alphanumeric characters.  It could also be that NVelocity is based on a version of Velocity that hadn't implemented parentheses around a directive....

          -Mike

           
      • Mike Bridge

        Mike Bridge - 2007-09-24

        I was just revisiting this; it is probably nicer to omit the "else" entirely:

        #set($nulltest="NOT NULL")
        #if($column.Nullable)#set($nulltest="NULL")#end 

          SELECT * FROM mytable WHERE mycol IS $nulltest

        Cheers,

        -Mike

         

Log in to post a comment.