2013-05-29 97 views
0

我想將解釋變量的結果賦值給另一個變量。Freemarker:將解釋變量賦值給其他變量

Freemarker提供了內置的?interpret來解釋一個變量持有一個ftl表達式。見http://freemarker.sourceforge.net/docs/ref_builtins_expert.html#ref_builtin_interpret

如果我做

[#if var1?has_content && var1?starts_with(r"${")] 
    [#assign interpretedValue = var1?interpret!""] 
    [@interpretedValue/] 
[/#if] 

[@interpretedValue/]將輸出解釋值。但是,我想將解釋值的值賦給一個變量(以便在我的代碼中執行一些操作,例如?has_content)。我試過[#assign varInterpretedValue = @interpretedValue]但這不起作用。

這可能嗎?

回答

2

是的,是這樣的:

[#assign capturedOutput][@(var1!'')?interpret /][/#assign] 
${capturedOutput} [#-- Attention! Put this into #noescape if you are inside #escape! --] 

注意,!'' SUFF具有對?interpret之前,否則它不會做任何事情(因爲?interpret結果總是非空)。

+0

您的解決方案几乎是正確的,並將我放在正確的道路上(請參閱我的答案中的解決方案)。嘗試此解決方案時,出現以下錯誤:var1!''?interpret不是用戶定義的指令。這是一個freemarker.template.SimpleScalar –

+1

正確...應該是'(var1!'')?interpret'。我已經更新了我的答案。 – ddekany