明文 Velocity 模板单行 foreach

发布于 2024-11-18 16:15:40 字数 841 浏览 1 评论 0原文

我正在使用速度模板为通知系统生成纯文本电子邮件。我有一个“收件人”POJO 列表,其中包含用户 ID 和电子邮件地址。我想迭代这些 POJO 并在一行中输出用户 ID(最终用逗号等分隔符分隔)。我在格式化输出时遇到问题。例如:

For Info:         #foreach( $addressee in $info_addressees )$addressee.userId #end
For Action:       #foreach( $addressee in $action_addressees )$addressee.userId #end

不幸的是,这不会在每个 foreach 循环后创建一个新行。所以我得到类似的东西:

信息:user1 user2 user3 操作:user4 user 5 user6

我似乎能够在每个 foreach 循环末尾添加新行的唯一方法是添加一些附加字符,例如:

For Info:         #foreach( $addressee in $info_addressees )$addressee.userId END #end
For Action:       #foreach( $addressee in $action_addressees )$addressee.userId END #end

我尝试使用空格最后,但似乎这些都被吞噬了,因此被忽略了(不是我所期望的)。

这样做的正确方法是什么?可以用宏来完成吗?

模板的输出必须是纯文本(不能像 HTML 那样结构化);并且,格式化必须在模板中完成,而不是在放入模板模型之前完成。

I'm using velocity templates to generated plain-text emails for a notification system. I have a list of "Addressee" POJOs which contain a user ID and email address. I want to iterate over these POJOs and output the user id in one line (and eventually separated by some delimiter such as commas). I'm having problems formatting the output. For instance:

For Info:         #foreach( $addressee in $info_addressees )$addressee.userId #end
For Action:       #foreach( $addressee in $action_addressees )$addressee.userId #end

Unfortunately this does not create a new line after each foreach loop. So i get something like:

For Info: user1 user2 user3 For Action: user4 user 5 user6

The only way I seem to be able to add a new line at the end of each foreach loop is by adding some additional character(s), such as:

For Info:         #foreach( $addressee in $info_addressees )$addressee.userId END #end
For Action:       #foreach( $addressee in $action_addressees )$addressee.userId END #end

I tried using whitespace at the end but it seems that these are gobbled and hence ignored (not what I expected).

What's the right way to do this? And could it be done with a macro?

The output from templating must be plaintext (it cannot be structured like HTML); and, the formatting must be done in the template and not in before being put into the template model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-11-25 16:15:40
#set( $n = '
')

#foreach($foo in $bar)$foo $n#end

or use the EscapeTool from the VelocityTools project

#foreach($foo in $bar)$foo $esc.n#end
#set( $n = '
')

#foreach($foo in $bar)$foo $n#end

or use the EscapeTool from the VelocityTools project

#foreach($foo in $bar)$foo $esc.n#end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文