【发布时间】:2019-11-28 22:28:10
【问题描述】:
我有 YAML file 和我的配置名称 applications.yaml,这些数据将是我的绑定:
applications:
- name: service1
port: 8080
path: /servier1
- name: service2
port: 8081
path: /service2
然后我有一个模板文件applications.config:
<% applications.each { application -> %>
ApplicationName: <%= application.name %>
<% } $ %>
把所有东西放在一起:
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()
现在的问题是:这个过程的输出是:
ApplicationName: service1
ApplicationName: service2
但我想要这个:
ApplicationName: service1
ApplicationName: service2
我不知道为什么那里有那些额外的空格。我想删除那些,但我看不到这些新的或断线是如何、何时或什么的。
谢谢。
【问题讨论】:
-
您尝试将应用程序配置模板代码全部放在一行上吗?
-
不,我想做的只是一行接一行,输出中没有多余的空格。 @tim_yates,正如你所看到的输出,正在循环中添加新行,但我不希望这样。
-
是的,这就是为什么我建议从模板中删除多余的换行符
-
为什么所有内容都在一行中,如果我想要一个 if/else 或嵌套循环会发生什么?我想知道是否有办法避免多余的空间,例如在 jinja2 我可以使用
{% my_value -%}其中 - 允许我跳过新行,但我想知道是否模板有类似的东西或配置。
标签: templates groovy template-engine gstring