Chef newline in ERB templates

I have a variable that I would like to cover in two lines when releasing in the ERB Chef template.

So, if node['apples'] is "Cortland, \nRed Delicious, \nGolden Delicious, \nEmpire, \nFuji, \nGala"

I want it to display as:

 Cortland, Red Delicious, Golden Delicious, Empire, Fuji, Gala 

when i call:

 <%= node['apples'] %> 

in my template. The newline character \n does not seem to work. How is this achieved?

Thanks!

+4
source share
2 answers

You can scroll through the list in the template instead of adding control characters to the string.

Recipe:

 $ cat test.rb node.set['apples'] = %W(Cortland Red\ Delicious Golden\ Delicious Empire Fuji Gala) template "test" do local true source "./test.erb" end 

A template using rubys join to add the characters you need at the end of each element:

 $ cat test.erb <%= node['apples'].join(",\n") %> 

Result:

 $ chef-apply test.rb Recipe: (chef-apply cookbook)::(chef-apply recipe) * template[test] action create - create new file test - update content in file test from none to 70d960 --- test 2015-08-02 12:32:23.000000000 -0500 +++ /var/folders/r6/9h72_qg11f18brxvpdpn6lbm0000gn/T/chef-rendered-template20150802-76337-130h5sl 2015-08-02 12:32:23.000000000 -0500 @@ -1 +1,8 @@ +Cortland, +Red, +Delicious, +Golden Delicious, +Empire, +Fuji, +Gala 

You can also look at Rubys split to turn a string into a list. http://ruby-doc.org/core-2.2.0/String.html#method-i-split

Edited to add in ,\n , as in the original request.

+1
source

I finally found a solution to this problem.

The scenario described in the question was solved simply by adding new line characters to indicate gaps in the variable.

So a variable declared as:

 "Cortland, " + 0x0D.chr + 0x0A.chr + "Red Delicious" 

will be output to the template as:

 Cortland, Red Delicious 
0
source

All Articles