如何使迭代输出在同一行?

发布于 2025-01-08 11:05:18 字数 876 浏览 0 评论 0原文

我想使用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 技术交流群。

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

发布评论

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

评论(2

网白 2025-01-15 11:05:18

另一种方法是像这样更改代码:

<#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/>)

也就是说,使列表全部内联。它更难阅读,但它会保留您需要的空白字符(即空格)。

Another way for you to do it there would be to change your code like this:

<#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/>)

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).

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