2016-02-07 71 views
0

我有这样的事情:Stringtemplate - 使用条件而不向输出添加换行符,并仍保持模板可读?

properties(attributeInfo) ::= << 
private <attributeInfo:parameters()>; 

>> 

parameters(attributeInfo) ::= << 
<if(attributeInfo.struct||attributeInfo.array)><attributeInfo:paramComposite()><else><javaTypeNameMap.(attributeInfo.typeName)> <attributeInfo.propertyName><endif> 
>> 

这将产生所需的输出:

private com.terradatum.common.db.model.terradatum.MlsAgentIdObj agentObj; 
private String officeName; 
private String officeAddress; 
private String officeCity; 
private String officeState; 
private String officeZipcode; 
private MlsPhoneTbl phoneTbl; 
private String agentEmail; 
private String agentAddress; 
private String agentCity; 
private String agentState; 
private String agentZipcode; 

当我改变parameters子模板以下几点:

parameters(attributeInfo) ::= << 
<if(attributeInfo.struct||attributeInfo.array)><attributeInfo:paramComposite()> 
<else><javaTypeNameMap.(attributeInfo.typeName)> <attributeInfo.propertyName> 
<endif> 
>> 

模板更为清晰可辨,但输出现在包含换行符:

private com.terradatum.common.db.model.terradatum.MlsAgentIdObj agentObj 
; 
private String officeName 
; 
private String officeAddress 
; 
private String officeCity 
; 
private String officeState 
; 
private String officeZipcode 
; 
private MlsPhoneTbl phoneTbl 
; 
private String agentEmail 
; 
private String agentAddress 
; 
private String agentCity 
; 
private String agentState 
; 
private String agentZipcode 
; 

我很困惑这种行为 - 基于我了解如何有条件地包含子模板以及条件WRT对换行符的行为,parameters子模板的两种形式应产生相同的输出。

显然我的理解是不正确的,所以我希望有人会给我一些指导。

回答

1

尝试:

parameters(attributeInfo) ::= <% 
<if(attributeInfo.struct||attributeInfo.array> 
    <attributeInfo:paramComposite()> 
<else><javaTypeNameMap.(attributeInfo.typeName)> 
    <attributeInfo.propertyName> 
<endif> 
%> 

<% ...%>让StringTemplate的忽略分离的空白。 << ...>>只会忽略前导和尾随的换行符。

在某些情况下,函数trim的调用也可能有所帮助。

+0

这样做绝对是个诀窍 - 它仍然是挑战,但绝对更清晰。 – rbellamy