简体   繁体   English

Ruby 模板缩进问题 - puppet

[英]Ruby template indentation issue - puppet

I'm unable to fix indentation for ruby template, I've spent hours on this and still couldn't get it to work as expected.我无法修复 ruby 模板的缩进,我在这上面花了几个小时,但仍然无法按预期工作。

Here is the template:这是模板:

<%= @log_path -%> {
  <%= @rotate_every %>
  rotate <%= @rotate_count %>
  compress
  <% if @delaycompress == true %>delaycompress
  <% end -%>missingok
  notifempty
  create <%= @create_mode -%> <%= @create_owner -%> <%= @create_group %>
  <% if @postrotate == true -%>postrotate
    <% @postrotate_cmd.each do |value| -%><%= value %>
  <% end -%>endscript<% end %>
}

When @postrotate is false the output looks like this.当@postrotate 为假时,output 看起来像这样。 The } is after a empty line. }在空行之后。

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
  
  }

When @postrotate is true the output looks like this.当@postrotate 为真时,output 看起来像这样。 The second invoke commnad should be a bit to the right.第二个invoke commnad应该在右边一点。

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
  postrotate
    invoke-rc.d rsyslog reload > /dev/null
  invoke-rc.d rsyslog reload > /dev/null
  endscript
  }

Expected output when @postrotate is false:当@postrotate 为假时,预期的 output:

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
}

Expected output when @postrotate is true:当@postrotate 为真时,预期的 output :

/var/log/auth.log {
  weekly
  rotate 4
  compress
  delaycompress
  missingok
  notifempty
  create 0644 root adm
  postrotate
    invoke-rc.d rsyslog reload > /dev/null
    invoke-rc.d rsyslog reload > /dev/null
  endscript
}

You seem to have incorrect expectations about the meaning of the -%> end-of-tag token.您似乎对-%> end-of-tag 标记的含义有错误的期望。 If you close a tag with -%> and it is the last non-whitespace on that line of the template, then any trailing whitespace and the newline following it are consumed instead of being copied to the output.如果您使用-%>关闭标签,并且它是模板该行上的最后一个非空格,则任何尾随空格和其后的换行符都会被消耗,而不是被复制到 output。 But that does nothing for you where the tag closed with -%> is not the last non-whitespace on the line, which is often the case in your template.但是,如果以-%>结尾的标记不是行上的最后一个非空格,这对您没有任何帮助,这在您的模板中通常是这种情况。

You also seem not to know about the <%- start-of-tag token, which has a similar effect on any leading indentation of a line where a tag starting with that token is the first non-whitespace.您似乎也不知道<%- start-of-tag 标记,它对以该标记开头的标记是第一个非空格的行的任何前导缩进具有类似的效果。 There are several places where using that could help you make your template easier to read.有几个地方使用它可以帮助您使您的模板更易于阅读。

Overall, I might write your template more like this:总的来说,我可能会写你的模板更像这样:

<%= @log_path %> {
  <%= @rotate_every %>
  rotate <%= @rotate_count %>
  compress
  <%- if @delaycompress -%>
  delaycompress
  <%- end -%>
  missingok
  notifempty
  create <%= @create_mode %> <%= @create_owner %> <%= @create_group %>
  <%- if @postrotate -%>
  postrotate
    <%- @postrotate_cmd.each do |value| -%>
    <%= value %>
    <%- end -%>
  endscript
  <%- end -%>
}

In particular, note that lines that contain nothing but whitespace and a non-printing tag that starts with <%- and ends with -%> produce no output at all.特别要注意的是,除了空格和以<%-开头并以-%>结尾的非打印标签以外的行,根本不会产生 output。 Using that to separate flow-control statements from template text and output tags makes the template clearer and easier to read, and it will also ensure that your loop body is consistently indented.使用它来将流控制语句与模板文本和 output 标记分开,使模板更清晰、更易于阅读,并且还可以确保循环体始终缩进。 Furthermore, it may help you see and very likely will help you debug issues with leaving off the - where you wanted it, as your original template does in its last <% end %> tag.此外,它可能会帮助您查看并且很可能帮助您调试问题,因为您希望它离开-就像您的原始模板在其最后一个<% end %>标记中所做的那样。

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

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