gpt4 book ai didi

chef-infra - ERB 模板中的 Chef 换行符

转载 作者:行者123 更新时间:2023-12-04 21:38:54 28 4
gpt4 key购买 nike

我有一个变量,我想在 Chef ERB 模板中输出时跨越两行。

所以如果 node['apples']等于 "Cortland, \nRed Delicious, \nGolden Delicious, \nEmpire, \nFuji, \nGala"
我希望它显示为:

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

当我打电话时:
<%= node['apples'] %>

在我的模板中。换行符, \n似乎不起作用。这种行为是如何实现的?

谢谢!

最佳答案

您可以遍历模板中的列表,而不是将控制字符添加到字符串中。

食谱:

$ cat test.rb 
node.set['apples'] = %W(Cortland Red\ Delicious Golden\ Delicious Empire Fuji Gala)

template "test" do
local true
source "./test.erb"
end

使用 ruby​​ 的模板 join在每个元素的末尾添加您需要的字符:
$ cat test.erb 
<%= node['apples'].join(",\n") %>

结果:
$ 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

你也可以看看 Rubys split将字符串转换为列表。 http://ruby-doc.org/core-2.2.0/String.html#method-i-split

编辑添加在 ,\n就像在原始请求中一样。

关于chef-infra - ERB 模板中的 Chef 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31767631/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com