Reputation: 3660
I have a helper that creates mailer template rows (html). I want to be able to pass in styles to the row (optionally), like a background color.
module MailHelper
def module_row(&block)
h << "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
# more table html here
h << capture(&block)
# more table html here
h << "</table>"
raw h
end
end
I want to be able to optionally pass in a background color, but I can't seem to figure out how to do that while passing in the '&block'. Is this possible in Ruby?
Upvotes: 0
Views: 476
Reputation: 18614
You can pass options as a Hash well, like:
module MailHelper
def module_row(**opts, &block)
bgcolor = opts[:bgcolor] || '#FFFFFF'
...
h << "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
# more table html here
h << capture(&block)
# more table html here
h << "</table>"
raw h
end
end
Then you can call:
module_row(bgcolor: '#AAAAAA', &my_block)
or:
module_row(bgcolor: '#AAAAAA') { block content }
Upvotes: 1
Reputation: 6041
You sure can!
module MailHelper
def module_row(options={}, &block)
...
if options[:foo]
do_foo_stuff
end
end
end
<% module_row(foo: true) do |x| %>
...
<% end %>
Common practice is to define defaults like this:
def module_row(options={}, &block)
opts = {
foo: true,
background_color: 'black'
}.merge!(options)
if opts[:foo]
do_foo_stuff
end
end
Upvotes: 1