I have been playing around with erb from the command line recently. I wanted to make a dirt simple erb template, for example the following:
最近我一直在用命令行中的erb。我想做一个非常简单的erb模板,例如:
<%- name = "Joe"; quality = "fantastic" -%>
Hello. My name is <%= name %>. I hope your day is <%= quality %>.
This works if I run
如果我跑的话,这个是有效的
erb -T - thatfile.erb
what I want to do is to make name
and quality
be passable from command line arguments, so that I could do something like:
我想要做的是使名称和质量能够通过命令行参数,这样我就可以做如下的事情:
./thatfile.erb "Bill" "super"
from the bash prompt and do the same thing.
从bash提示符执行相同的操作。
I am aware that I could write a ruby script that would just read that template in and then use ERB.new(File.read("thatfile.erb")).result(binding)
, or writing the template after an END
and doing likewise, but I'm looking for a more lightweight approach if it exists, because I don't want to write two files for each erb script that I create for this purpose.
我知道我可以写一个ruby脚本,就会读到模板,然后使用ERB.new(以(“thatfile.erb”)).result(绑定),结束后或写作模板,做同样的事情,但是我寻找一个更轻量级的方法如果它存在,因为我不想写每个erb脚本,我创建了两个文件。
4 个解决方案
#1
11
If you are using unix, try following:
如果您正在使用unix,请尝试以下操作:
$ cat 1.erb
Hello. My name is <%= name %>. I hope your day is <%= quality %>.
$ (echo '<% name="Joe"; quality="fantastic" %>' && cat 1.erb) | erb
Hello. My name is Joe. I hope your day is fantastic.
#2
18
Alternatively, you can use a ruby script and load it in as a library.
或者,您可以使用ruby脚本并将其作为库加载。
# vars.rb
@hello = 'kirk'
# template.html.erb
<div><%= @hello %></div>
$ erb -r './vars' template.html.erb
#3
15
I went with the BASH command-line shortcut for environmental variables.
我使用BASH命令行快捷方式处理环境变量。
Outside:
外:
STUFF=foo,bar erb input.html.erb >output.html
Inside:
内部:
<%
stuff = ENV['STUFF'].split(',')
%>
After a few minutes e-searching I determined the other solutions are all variations on "write the erb wrapper command yourself." Could be wrong, but I ain't going back.
经过几分钟的电子搜索,我确定其他的解决方案都是“编写erb包装器命令自己”的变体。可能是错的,但我不会回去。
#4
6
Please note that Ruby 2.2 and newer provide a much nicer solution that was implemented according to this:
请注意,Ruby 2.2和更新版本提供了一个更好的解决方案,它是根据以下内容实现的:
erb var1=val1 var2=val2 my-template.erb
#1
11
If you are using unix, try following:
如果您正在使用unix,请尝试以下操作:
$ cat 1.erb
Hello. My name is <%= name %>. I hope your day is <%= quality %>.
$ (echo '<% name="Joe"; quality="fantastic" %>' && cat 1.erb) | erb
Hello. My name is Joe. I hope your day is fantastic.
#2
18
Alternatively, you can use a ruby script and load it in as a library.
或者,您可以使用ruby脚本并将其作为库加载。
# vars.rb
@hello = 'kirk'
# template.html.erb
<div><%= @hello %></div>
$ erb -r './vars' template.html.erb
#3
15
I went with the BASH command-line shortcut for environmental variables.
我使用BASH命令行快捷方式处理环境变量。
Outside:
外:
STUFF=foo,bar erb input.html.erb >output.html
Inside:
内部:
<%
stuff = ENV['STUFF'].split(',')
%>
After a few minutes e-searching I determined the other solutions are all variations on "write the erb wrapper command yourself." Could be wrong, but I ain't going back.
经过几分钟的电子搜索,我确定其他的解决方案都是“编写erb包装器命令自己”的变体。可能是错的,但我不会回去。
#4
6
Please note that Ruby 2.2 and newer provide a much nicer solution that was implemented according to this:
请注意,Ruby 2.2和更新版本提供了一个更好的解决方案,它是根据以下内容实现的:
erb var1=val1 var2=val2 my-template.erb