Proc inside ERB - ruby

There's something weird going on here that I can not understand when I try to implement multiple content_for blocks in ERB using plain Ruby.
Here is my code:
# helper.rb
require 'erb'
def render(path)
ERB.new(File.read(path)).result(binding)
end
def content_for(key, &block)
content_blocks[key.to_sym] = block
end
def yield_content(key)
content_blocks[key.to_sym].call
end
def content_blocks
#content_blocks ||= Hash.new
end
And template:
<% #test.html.erb %>
<% content_for :style do %>
style
<% end %>
<% content_for :body do %>
body
<% end %>
<% content_for :script do %>
script
<% end %>
When I open irb to test, I get
irb(main):001:0> require './helper'
=> true
irb(main):002:0> render 'test.html.erb'
=> "\n\n\n"
irb(main):003:0> content_blocks
=> {:style=>#<Proc:0x005645a6de2b18#(erb):2>, :body=>#<Proc:0x005645a6de2aa0#(erb):5>, :script=>#<Proc:0x005645a6de2a28#(erb):8>}
irb(main):004:0> yield_content :script
=> "\n\n\n\n script\n"
irb(main):005:0> yield_content :style
=> "\n\n\n\n script\n\n style\n"
Why yield_content :script got the \n\n\n prepended and why yield_content :style got the script\n\n in result.

If you do
ERB.new(File.read(path)).src
Then you can see what erb compiles your template to. The template ends up looking like (formatted for readability)
_erbout = ''
_erbout.concat "\n"
#test.html.erb
_erbout.concat "\n"
content_for :style do
_erbout.concat "\n style\n"
end
Where _erbout is the buffer that accumulates the output from your template. _erbout is just a local variable,m
When you call your procs they are all just appending to that same buffer, which already contains the result from the template render.

Related

Ruby Iterate from an object using ERB

I'm new, was tasked to do a front end job but I thought i can figure out ERB easily.
In my app.rb
variables defined
def find_listings
3.times.map { random_item }
end
def random_item
{
name: 'blah',
location: 'LA'
}
end
get '/' do
#listings = find_listings
erb :index
end
In my ERB file I am trying to iterate
index.erb
<% #listings.each do |value | %>
<strong><%= value %><br/>
<%# This returns {:name=>"blah", :location=>"New York"} %>
<strong><%= value['name']%></strong><br/>
<%# This returns nothing %>
<% end %>
How do i get the name value?
Your hash has symbols as keys (not strings), so you can access name this way: value[:name].

Only execute code in erb if variable exists?

Ruby newbie here who just started using Ruby with .erb templates and I'm having a problem with the code. I have a hangman game that's passing variables from the .rb file to the .erb file and everything was working fine until I tried to check it on initial load (no variables present) and it threw errors. So I figured I'd use defined? with an if statement to check if the variable exists and then execute the code if it does and ignore if doesn't. It works fine when I use:
<% if defined?bad_guesses %>
<%= bad_guesses %>
<% end %>
But the information I need is an array and when I try to use an .each or .times statement like this:
<% if defined?bad_guesses %>
<% bad_guesses.each do |i| %>
<%= i %>
<% end %>
<% end %>
I get:
NoMethodError at /
undefined method `each' for nil:NilClass
C:/Projects/hangman/views/index.erb in block in singleton class
<% bad_guesses.each do |i| %> hangman.rb in block in
erb :index, :locals => {:game_status => game_status, :bad_guesses => bad_guesses, :good_guesses => good_guesses, :word => word}
Any suggestions appreciated.
Also, is this even the proper way to do this? When you make an .erb template that uses variables passed in from a class in your .rb file, how do you ignore it until it exists to the template?
Passing variables using:
get '/' do
if params['make'] != nil
make = params['make'].to_i
game_status, bad_guesses, good_guesses, word = Hangman.get_word(make)
elsif params['guess'] != nil
guess = params['guess'].to_s
game_status, bad_guesses, good_guesses, word = Hangman.check_guess(guess)
end
erb :index, :locals => {:game_status => game_status, :bad_guesses => bad_guesses, :good_guesses => good_guesses, :word => word}
end
Looking at this:
<% if defined?bad_guesses %>
<% bad_guesses.each do |i| %>
<%= i %>
<% end %>
<% end %>
a few points:
defined?a is bad style; use defined?(bad_guesses) or defined? bad_guesses instead.
defined? checks if it's defined, so if you say foo = nil; defined? foo it will be true.
You could alternatively use this:
defined?(bad_guesses) && bad_guesses
On the other hand, undefined instance variables are nil by default:
# it shows undefined
defined? #non_existing_var
# but you can still check if it's truthy:
puts "found" if #non_existing_var
# it won't print anything
Similar to instance variables in this regard are hashes. The default value of an unknown key is nil.
The problem with instance variables is they are not scoped to the partial. Instead, I recommend sending your local variables as a nested hash:
locals: { data: { foo: "bar" } }
Then you can safely check for values which may not exist:
if data[:foo]
# this runs
elsif data[:non_existent]
# this doesnt run
end
For my purposes, the following syntax worked:
<% if some_var %>
<%= some_var %>
<% end %>
This block is rendered if some_var is not nil.

Passing parameters to erb view

I'm trying to pass parameters to an erb view using Ruby and Sinatra.
For example, I can do:
get '/hello/:name' do
"Hello #{params[:name]}!"
end
How do I pass :name to the view?
get '/hello/:name' do
erb :hello
end
And how do I read the parameters inside view/hello.erb?
Thanks!
just pass the :locals to the erb() in your routes:
get '/hello/:name' do
erb :hello, :locals => {:name => params[:name]}
end
and then just use it in the views/hello.erb:
Hello <%= name %>
(tested on sinatra 1.2.6)
Not sure if this is the best way, but it worked:
get '/hello/:name' do
#name = params[:name]
erb :hello
end
Then, I can access :name in hello.erb using the variable #name
get '/hello/:name' do
"Hello #{params[:name]}!"
end
You cannot do this in routes.
You want to set the params in the controller.
app/controllers/some_controller.rb
def index
params[:name] = "Codeglot"
params[:name] = "iPhone"
params[:name] = "Mac Book"
end
app/views/index.html.erb
<%= params[:name] %>
<%= params[:phone] %>
<%= params[:computer] %>

hash enumeration and using outside arguments

I have a form builder and the normal |f| variable. In the middle I'd like to iterate over some hash and get a list of keys by which to build the form. How can I incorporate "f" as an argument to the enumerator.
<%= form_for ..... do |f| %>
<% available_types.each do |k,v| %>
<%= f.text_filed :selection, :value => v %>
<% end %>
<% end %>
As you can see in the code above, "f" is outside of the scope. Ideas?
f should still be in the scope. You misspelled text_field though.

How do I html_escape text data in a sinatra app?

I have a small Sinatra app which generates html fragments for me from an ERB template.
How do I html_escape the output?
The <%=h somestring %> helper does not exist in Sinatra.
Rack::Utils includes a HTML escape method. http://www.sinatrarb.com/faq.html#escape_html
require 'CGI'
get '/html' do
erb :view
end
def h(html)
CGI.escapeHTML html
end
__END__
##view
<% File.open('my.html') do |f| %>
<%=h f.read() %>
<% end %>

Resources