How can I pass String to a function in NVelocity template?

I am using NVelocity binding mechanism to generate fixed length > field output - you know what this is:

Field        Start Pos   Field Length  Notes
----------   ---------   ------------  ---------
Supplier      1           7            Leading Zeros
GRN           8           9            -
...

e.g.
>0001234    123A<

      

Problem I'm trying to call String.PadRight () with an overload to specify a leading zero, and NVelocity doesn't have that.

It works:

$Document.SupplierCode.PadRight(7)

      

But this is not the case:

$Document.SupplierCode.PadRight(7,"0")

      

I tried:

  • Single quotes ( '0'

    )

  • Double single quotes ( ''0''

    )

  • Double quotes ( "0"

    )

  • Double double quotes ( ""0""

    )

  • Reset quotes for all of the above ( \"0\"

    )

  • No quotes!

All I have found to work is the NVelocity Homepage , and Link to the speed language man page , niether points me to a solution.

Sorry that I cannot point or point you somewhere where you can test your ideas for yourself, but any suggestions you may have will be most welcome!

Thanks for the help; o)

0


source to share


2 answers


One solution that a colleague has come across is to create another object in the Document object that returns a formatted string:

eg.



Public ReadOnly Property SupplierCodeFormatted() As String
    Get
        Return Supplier.Code.PadLeft(7, "0")
    End Get
End Property

      

0


source


I am dealing with the same problem at the moment, as far as I understand it is because the PadLeft and PadRight functions of the String class receive a second parameter leading "0" as a char and not as a string.

NVelocity allows you to specify a parameter as a string using "0", but this way internally it throws a casting exception (or something similar) since the parameter is expected to be a char.



I haven't found yet (I'm just using NVelocity since 1 o'clock!) To specify the parameter as char, at the moment I only have a dirty solution like applying Replace ("", "0") after PadLeft / PadRight, so the template becomes

$ Document.SupplierCode.PadRight (7) .Replace ('', '0')

+1


source







All Articles