简体   繁体   English

如何使用Groovy脚本引擎替换三元运算中的变量

[英]How to replace variable inside ternaryoperation using groovy script engine

I am using Groovy SimpleTemplateEngine to set values dynamically at run time. 我正在使用Groovy SimpleTemplateEngine在运行时动态设置值。 I am using ternary operator as well inside the string. 我也在字符串中使用三元运算符。 Values are not getting updated for the variables inside the ternary operator. 三元运算符中变量的值不会更新。 Can someone please help how to achieve this? 有人可以帮忙实现这一目标吗?

File f = new File("test.txt");
        SimpleTemplateEngine engine = new SimpleTemplateEngine();
        Template template = engine.createTemplate(f);
        def refMap = [:]
        refMap["condition1"] = "true";
        refMap["acctNbr"] = "1234567890";
        refMap["value"] = "abc";
        println template.make(refMap).toString();

test.txt

<acctNbr13>${acctNbr}</acctNbr13>
${(
Boolean.parseBoolean(condition1)
?
'''
<test>${value}</test>
'''
:
''
)}

I suspect the String already represents the replaced value and does not get parsed itself. 我怀疑字符串已经代表了替换后的值,并且自身未得到解析。

Would it work for you to replace 它适合您更换吗

 '''
 <test>${value}</test>
 '''

with

 '<test>' + value + '</test>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM