ruby Chef 和 erb 模板。如何使用布尔代码块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9935905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chef and erb templates. How to use boolean code blocks
提问by Tampa
I am new to chef, ruby, ruby DSL, and erb. I come from python. In a ruby erb template I want to do something like this.
我是厨师、红宝石、红宝石 DSL 和 erb 的新手。我来自蟒蛇。在 ruby erb 模板中,我想做这样的事情。
<% if node[:monit][:server]=='nginx' -%>
ALL OF MY NGINX TEXT
<% end -%>
<% if node[:monit][:server]=='redis' -%>
ALL OF MY REDIS TEXT
<% end -%>
Clearly I am missing something about proper syntax.
显然,我遗漏了一些有关正确语法的信息。
Thanks
谢谢
回答by Joshua Clark
Try this:
尝试这个:
<% if node[:monit][:server]=='nginx' -%>
nginx_text=<%= node[:nginx][:text] %>
<% end -%>
<% if node[:monit][:server]=='redis' -%>
redis_text=<%= node[:redis][:text] %>
<% end -%>
Code wrapped in <% %>or <% -%>is a statement that is evaluated. Code wrapped in <%= %>is code that is evaluated and the result is placed into the file. Harcoded strings dont have to be wrapped in erb tags if they are constant, but Ruby code must be wrapped in erb tags if you want the result of that code to go into your file
包含在<% %>或<% -%>中的代码是一个被评估的语句。封装在<%= %>代码中的是被评估并将结果放入文件中的代码。如果硬编码字符串是常量,则不必将它们包装在 erb 标签中,但如果您希望该代码的结果进入您的文件,则必须将 Ruby 代码包装在 erb 标签中

