2010-11-16 111 views
4

有人可以解釋爲什麼這不起作用嗎?Stringtemplate比較字符串不起作用

StringTemplate query = new StringTemplate("hello " + 
       "$if(param==\"val1\")$" + 
       " it works! " + 
       "$endif$ " + 
       "world"); 
     query.setAttribute("param", "val1"); 
     System.out.println("result: "+query.toString()); 

它將引發

EVAL樹解析錯誤 :0:0:在org.antlr.stringtemplate.language.ActionEvaluator.ifCondition(ActionEvaluator.java:815) 子樹 意外結束在有機.antlr.stringtemplate.language.ConditionalExpr.write(ConditionalExpr.java:99)

回答

10

ST不允許在模板中進行計算。這會使它成爲模型的一部分。

+2

它嚴重地讓我想起人們如何找到StringTemplate,讀了足夠的關於創建示例的例子,但沒有掌握它最重要的一個優勢。 – I82Much 2010-11-17 18:47:08

+5

很難說它是真正的優點還是缺點。很容易看出爲什麼這不是那麼明顯:其他模板引擎有它。檢查相等性不是一個「計算」,再加上字符串模板中有條件。 – mvmn 2012-07-04 11:43:23

+1

條件只測試是否存在,而不是值。在模型視圖分離方面存在巨大差異。 – 2012-07-04 18:50:05

2
StringTemplate query = new StringTemplate("hello " + 
       "$if(paramEquals)$" + 
       " it works! " + 
       "$endif$ " + 
       "world"); 
     query.setAttribute("paramEquals", param.equals("val1")); 
     System.out.println("result: "+query.toString());