Search code examples
stringnvelocitypadleft

How do I pass a String into a function in an NVelocity Template?


I'm using the NVelocity Templating engine to produce a fixed-length field output - you know the kind of thing:

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

e.g.
>0001234    123A<

The problem is I'm trying to call String.PadRight() with the overload to specify the leading zero, and NVelocity is having none of it..

This works:

$Document.SupplierCode.PadRight(7)

But this doesn't:

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

I've tried:

  • Single Quotes ('0')

  • Double Single-Quotes (''0'')

  • Double Quotes ("0")

  • Double Double-Quotes (""0"")

  • Escaping the quotes for all of the above (\"0\")

  • No Quotes!

All I've found to work from is the NVelocity Homepage, and the Velocity Templating Language Reference page, niether are pointing me at a solution.

Sorry I'm unable to supply or point you somewhere where you can test out your ideas for yourself, but any suggestions you may have will be most welcome!

Thanks for your help ;o)


Solution

  • One solution that a colleague has come up with is to create another property in the Document object that returns the formatted String:

    E.g.

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