Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 447 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing binding or arguments to ERB from the command line

#1
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

what I want to do is to make <code>name</code> and <code>quality</code> 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.

I am aware that I could write a ruby script that would just read that template in and then use <code>ERB.new(File.read("thatfile.erb")).result(binding)</code>, or writing the template after an <code>__END__</code> 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.
Reply

#2
I went with the BASH command-line shortcut for environmental variables.

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

#3
Please note that Ruby 2.2 and newer provide a much nicer solution that was implemented according to [this][1]:

erb var1=val1 var2=val2 my-template.erb


[1]:

[To see links please register here]

Reply

#4
Alternatively, you can use a ruby script and load it in as a library.

<!-- language: lang-rb -->

# vars.rb
@hello = 'kirk'

<!-- language: lang-erb -->

# template.html.erb
<div><%= @hello %></div>

<!-- language: lang-sh -->

$ erb -r './vars' template.html.erb
Reply

#5
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.

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through