如何使迭代输出在同一行?
我想使用freemarker生成一些java代码,即为一个方法生成参数。假设我有一个名为doIt的方法,它需要一些参数名称及其类名称,我将给模板一个名为paramList的参数。我定义一个宏指令,迭代参数列表,但每个参数占一行。我的模板代码如下:
<#macro paramList plist>
<#if plist??>
<#list plist as p>
${p.javaType?substring(2)} ${p.name} <#if p_has_next>, </#if>
</#list>
</#if>
</#macro>
doIt(<@paramList plist=params/>)
运行结果是:
doIt( int end ,
String endDate ,
String evtCode ,
int evtNo ,
String giftCode ,
int start ,
String startDate
)
如何让所有参数输出出现在同一行。我知道我可以将列表指令逻辑写在同一行中以避免换行,但如果还有其他逻辑,一段时间后阅读和理解就会变得太长。 我想要的格式是:
doIt(int end , String endDate, String evtCode , int evtNo , String giftCode , int start , String startDate)
I want to generate some java code using freemarker, that is to generate parameters for a method.Say I have a method named doIt, which needs some parameter name and their class names, I will give the template a param named paramList.I define a macro directive , iterate the parameters list,but consequently each parameter occupies a row. My template code is as below:
<#macro paramList plist>
<#if plist??>
<#list plist as p>
${p.javaType?substring(2)} ${p.name} <#if p_has_next>, </#if>
</#list>
</#if>
</#macro>
doIt(<@paramList plist=params/>)
The running result is :
doIt( int end ,
String endDate ,
String evtCode ,
int evtNo ,
String giftCode ,
int start ,
String startDate
)
How to make all the parameters output appear in the same row. I know I can write the list directive logic in the same row to avoid line breaking ,but if there are other logic too, it will get too long to read and understand after a while.
The format I want is :
doIt(int end , String endDate, String evtCode , int evtNo , String giftCode , int start , String startDate)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在最里面的
之后放置
<#t>
。 (参见http://freemarker.org/docs/ref_directive_t.html#ref.directive .t)Put a
<#t>
after the innermost</#if>
. (See http://freemarker.org/docs/ref_directive_t.html#ref.directive.t)另一种方法是像这样更改代码:
也就是说,使列表全部内联。它更难阅读,但它会保留您需要的空白字符(即空格)。
Another way for you to do it there would be to change your code like this:
That is, make the list all inline. It's harder to read, but it will keep your white space characters that you need (i.e. the spaces).