【问题标题】:Plaintext Velocity template single-line foreach明文 Velocity 模板单行 foreach
【发布时间】:2011-09-28 20:08:43
【问题描述】:

我正在使用速度模板为通知系统生成纯文本电子邮件。我有一个包含用户 ID 和电子邮件地址的“收件人”POJO 列表。我想遍历这些 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 那样结构化);并且,格式化必须在模板中完成,而不是在放入模板模型之前。

【问题讨论】:

    标签: java formatting velocity template-engine


    【解决方案1】:
    #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
    

    【讨论】:

    • er.. 把 $n 或 $esc.n 放在 #end 之后
    • 干杯。实际上,我自己也提出了类似的解决方案,但一直没有时间发布它。
    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2013-07-03
    • 2018-05-29
    相关资源
    最近更新 更多