如何将字符串传递到 NVelocity 模板中的函数中?
我正在使用 NVelocity 模板引擎 来生成固定- length 字段输出 - 你知道这样的事情:
Field Start Pos Field Length Notes
---------- --------- ------------ ---------
Supplier 1 7 Leading Zeros
GRN 8 9 -
...
e.g.
>0001234 123A<
问题是 我正在尝试使用重载来调用 String.PadRight() 来指定前导零,而 NVelocity 没有这些。
这有效:
$Document.SupplierCode.PadRight(7)
但这不行:
$Document.SupplierCode.PadRight(7,"0")
我已经尝试过:
单引号 (
'0 '
)双单引号 (
''0''
)双引号 (
"0"
)双双引号 (
""0""
)转义上述所有内容的引号 (
\"0\"
)否引号!
我发现可以使用的只是 NVelocity 主页 和 Velocity 模板语言参考页面,都没有向我指出解决方案。
抱歉,我无法向您提供或指出您可以自己测试您的想法的地方,但我们非常欢迎您提出任何建议!
感谢您的帮助;o)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我目前正在处理同样的问题,据我了解,这是由于 String 类的 PadLeft 和 PadRight 函数接收第二个参数,即前导“0”,作为字符,而不是字符串。
NVelocity 允许您使用“0”将参数指定为字符串,但通过这种方式,它会在内部生成强制转换异常(或类似的异常),因为参数应为 char。
我还没有找到(我从 1 小时开始就使用 NVelocity!)一种将参数指定为 char 的方法,目前我只有一个肮脏的解决方案,例如在之后应用 Replace(" ", "0") PadLeft / PadRight,因此模板变为
$Document.SupplierCode.PadRight(7).Replace(' ', '0')
I'm coping with the same problem at the moment, as far as I understand it is due to the fact that PadLeft and PadRight functions of String class receive the second parameter, the leading "0", as a char, not as a string.
NVelocity allows you to specify the parameter as a string using '0', but in this way internally it generate a cast exception (or something similar), because the parameter is expected as char.
I haven't found yet (I'm just using NVelocity since 1 hour!) a way to specify the parameter as char, at the moment I have just a dirty solution such as applying a Replace(" ", "0") after the PadLeft / PadRight, so the template becomes
$Document.SupplierCode.PadRight(7).Replace(' ', '0')
一位同事提出的一个解决方案是在 Document 对象中创建另一个返回格式化字符串的属性:
例如
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.