【发布时间】:2017-05-17 12:39:29
【问题描述】:
我有一个带有占位符的模板(例如 ${PARAM1}),程序成功地解决了它们。但是,如果我只想解析传递给模板引擎的占位符并忽略其他 ${} 该怎么办?目前,如果程序无法解析所有占位符,则会失败。
static void main(String[] args) {
def template = this.getClass().getResource('/MyFile.txt').text
def parameters = [
"PARAM1": "VALUE1",
"PARAM2": "VALUE2"
]
def templateEngine = new SimpleTemplateEngine()
def output = templateEngine.createTemplate(template).make(parameters)
print output
}
文件:${PARAM1} ${PARAM2} ${PARAM3}
谢谢
【问题讨论】:
-
你能写出预期的输出吗?
-
预期输出:VALUE1 VALUE2 ${PARAM3}
-
老实说,我不确定 groovy 模板引擎是否支持忽略参数的方式;但是您可以将您的参数更改为:
def parameters = [ "PARAM1": "VALUE1", "PARAM2": "VALUE2", "PARAM3": "\${PARAM3}" ]这将为您提供预期的输出。 -
我刚试过,它有效。但是我需要生成大型构建脚本并且只解决部分占位符的问题 - 并且两次编写所有占位符很无聊)。可能我会尝试使用freemarker
-
你有地图形式的参数我可以帮你自动放置占位符