Search code examples
templateschef-infrarecipe

Chef Template Whitespace Formatting


I have a hash of key:value pairs that is to be printed. All of the keys are Strings of varying length, and for readability, I would like the output to be formatted in two columns.

<% @foo.each do |bar| %>
    <%= bar['key'] %>  <%= bar['value'] %>
<% end %>

Is it possible to accomplish this, either through a setting in my recipe or on the template itself?


Solution

  • On the non-printing tags (i.e. <% %>) you can use - to trim whitespace. <%- will remove whitespace before the tag and -%> will remove whitespace after the tag. If you do <%- @foo.each do |bar| -%> and <%- end -%> it will remove the blank lines entirely. How you do the padding for the printing lines, look at the Ruby methods ljust and rjust.