从命令行将绑定或参数传递给ERB [英] Passing binding or arguments to ERB from the command line

查看:79
本文介绍了从命令行将绑定或参数传递给ERB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在命令行中使用erb。我想制作一个简单的erb模板,例如:

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:

<%- name = "Joe"; quality = "fantastic" -%>
Hello. My name is <%= name %>. I hope your day is <%= quality %>.

如果我跑步,则可以使用

This works if I run

erb -T - thatfile.erb

我想做的是使名称 quality 可以从命令行参数传递,这样我可以执行以下操作:

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"

从bash提示符处执行相同的操作。

from the bash prompt and do the same thing.

我知道我可以编写一个ruby脚本来读取模板,然后使用 ERB.new(File.read( thatfile.erb))。result(binding),或在 END ,但我正在寻找一种更轻量级的方法(如果存在),因为我不想为每个erb脚本编写两个文件

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.

推荐答案

如果您使用的是Unix,请尝试以下操作:

If you are using unix, try following:

$ 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.

这篇关于从命令行将绑定或参数传递给ERB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆