2016-02-07 74 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