2016-01-13 64 views
2

我在Java中使用StringTemplate 4.0.8。在StringTemplate中使用動態條目的詞典

StringTemplate-4 documentation,它說,

字典的字符串也可以是可以參考的屬性 一旦 字典值已經嵌入在模板中,這將成爲通過屬性的動態作用域可見模板。

我到底該怎麼做?我可以做這樣的事情:

output(input) ::= "The output is: <aDicitionary.input>" 

aDictionary ::= [ 
    "someKey":"someValue", 
    "someOtherKey":"someOtherValue", 
    "aCertainKey": **HERE** i want the value to be <input>, 
    default:"doesnt matter" 
] 

這樣output("someKey")導致The output is: someValueoutput(aCertainKey)結果「的輸出是:aCertainKey」。如果是這樣,語法看起來如何?

我知道我可以通過在一種情況下不傳遞輸入然後檢查是否有輸入來實現。但是,這會導致很多的,如果在Java端我

回答

2

要使用動態辭典條目的:

output(input) ::= <%The output is: <aDicitionary.(input)>%> 

周圍使用模板沒有報價,並把input括號中對其進行評估。

要具有在字典中的動態內容(所引用的塊的主題):

aDictionary ::= [ 
    "someKey":"someValue", 
    "someOtherKey":"someOtherValue", 
    "aCertainKey": {input from scope <inputFromScope>}, 
    default:"doesnt matter" 
] 

圍繞密鑰和變量(或模板)的引用內使用大括號。現在呼籲

<output(input="aCertainKey", inputFromScope="myInput")> 

將輸出

The output is: input from scope myInput 
+0

正是我需要的!非常感謝! :-) – Colophonius