【问题标题】:Avoid new break line in groovy template避免 groovy 模板中的新断线
【发布时间】: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


【解决方案1】:

例如从 notepad++(JSP 语言)查看您的模板:

在第一行的末尾&lt;% applications.each { application -&gt; %&gt;有一个CRLF会在每个Application之前打印出来

在下一行中,另一个CRLF 将在每个Application 之后打印。

所以你在每两个应用程序之间有两个CRLF

【讨论】:

  • 如何从我的 vscode 中删除这些?我正在尝试删除那些?
  • 很遗憾,标准模板不支持-%&gt; 抑制换行。因此,如果您想保留多行模板,请隐藏不应打印到 &lt;% ... %&gt; 代码部分的换行符。像这样:CRLF %&gt;Application: ... 或在这种情况下使用单衬:&lt;% applications.each { println "ApplicationName: $it" } %&gt;
  • 所有内容都在一行中?当我想使用 if/else 或更复杂的结构时会发生什么?真的很遗憾,没有办法避免这种情况!
【解决方案2】:

我观察到(来自答案、cmets 和一些实践)是: 我们在文件applications.config 中创建的所有new Lines 在输出中都被视为new line。正如 daggett 所说,这是默认设置。

所以在这里我只想展示可能的config 文件格式,可以在哪里应用一些conditional logic,就像你问的那样,对我来说看起来不错。例如:if()

application.config:

<% applications.each { application ->
 if (application.valid) {%>\
Type :<%=application.valid%>
ApplicationName:<%=application.name%>
Path:<%=application.path%>
Port:<%=application.port%>
<%} else{%>\
--------------------
Found invalid Application : <%= application.name %>
--------------------\
<%}}%>

application.yaml

applications:
- name: service1
  port: 8080
  path: /servier1
  valid: true
- name: service2
  port: 8081
  path: /service2
  valid: false

code.groovy

@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()

输出:

Type :true
ApplicationName:service1
Path:/servier1
Port:8080
--------------------
Found invalid Application : service2
--------------------

【讨论】:

  • 谢谢你们的回答,但这个@Bhanuchander 是我正在寻找的解决方案。使用\ 工作得非常好,还允许我删除其他地方的换行符。
【解决方案3】:

避免换行符的唯一方法是修复您的模板,如下所示

<% applications.each { application ->  %>ApplicationName: <%= application.name %>
<% } $ %>

结果

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2

或在下面的代码中处理换行符

@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)
def output = template.toString().replaceAll(/^\n|\n$/, "").replaceAll(/\n{2,}/,"\n")
println output

结果

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多