Use a variable name in an erb include call - ruby

I'm using Sinatra and I'd like to include another erb file in one of my views but using a variable for the file name.
I was thinking something like this may work but I get an error saying no such file could be found.
<%= erb :'layout/nav/#{device_type}' %>
Currently I'm using the below switch statement to achieve the desired results but the above would be cleaner and less code.
<% case device_type
when 'mobile'%>
<%= erb :'layout/nav/mobile' %>
<% when 'tablet' %>
<%= erb :'layout/nav/tablet' %>
<% else %>
<%= erb :'layout/nav/desktop' %>
<% end %>
Thanks
Giles

I think that could work, you just need to use double quotes to interpolate strings.
Try
<%= erb :"layout/nav/#{device_type}" %>

Related

Ruby: capture and reuse block of HTML code in ERB template

Note that I cannot use Rails or Javascript.
Is there any way to repeat a block of HTML code using only Ruby / ERB?
In Rails I would do this:
<% content_for :mycontent do %>
some html code
that I want to capture
etc
<% end %>
Then, here, in the same template, I can reuse the block...
<%= content_for :mycontent %>
Multiple times...
<%= content_for :mycontent %>
Is there any equivalent without Rails?

erb idiom to handle undefined variable

I'm trying to write some puppet .erb, I'd like to handle this "environment" variable if it's:
undefined
a string with newlines
an array.
I've got as far as this:
<% Array(environment).join("\n").split(%r{\n}).each do |f| %>
one line: <%= f %>
<% end %>
But haven't gotten around the undefined case yet. I've tried this
<% if (defined?(environment)).nil? %?
<% Array(environment).join("\n").split(%r{\n}).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
but am still getting "(erb):11: undefined local variable or method `environment' for main:Object (NameError)" when trying to test it like this:
ruby -rerb -e "environmentUNDEFINEME= [ 'cronvar=cronval', 'var2=val2' ];
puts ERB.new(File.read('templates/job.erb')).result"
Sorry this is so basic, but somebody's got to ask the easy questions. Any help?
I would do this:
<% if defined?(environment) %>
<% Array(environment).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
I didn't understand why you joining on new lines and then splitting on them again, so I removed it from the example.

How do I perform inline calculations on two variables within an .erb file?

I have the following .erb view in a Sinatra app:
<% sessions.each do |session| %>
<%= session.balance_beginning %>
<%= session.balance_ending %>
<% end %>
It works as expected, displaying the beginning and ending balances recorded for each session. I would like to calculate the net balances from within the .erb file, but I can't figure out how to do it. I have tried variations of this:
<% sessions.each do |session| %>
<%= session.balance_ending - session.balance_beginning %>
<% end %>
That doesn't work. I receive the following error in Sinatra:
undefined method `-' for nil:NilClass
How do I do what I'm trying to do?
Right #Zabba, in this case I think you would add a method to your Session model so you could call session.net_balance.
Then in your balance_ending and balance_beginning methods you would want to handle nil, either raise an error or return zero if that is valid.

Chef and erb templates. How to use boolean code blocks

I am new to chef, ruby, ruby DSL, and erb. I come from python.
In a ruby erb template I want to do something like this.
<% if node[:monit][:server]=='nginx' -%>
ALL OF MY NGINX TEXT
<% end -%>
<% if node[:monit][:server]=='redis' -%>
ALL OF MY REDIS TEXT
<% end -%>
Clearly I am missing something about proper syntax.
Thanks
Try this:
<% if node[:monit][:server]=='nginx' -%>
nginx_text=<%= node[:nginx][:text] %>
<% end -%>
<% if node[:monit][:server]=='redis' -%>
redis_text=<%= node[:redis][:text] %>
<% end -%>
Code wrapped in <% %> or <% -%> is a statement that is evaluated. Code wrapped in <%= %> is code that is evaluated and the result is placed into the file. Harcoded strings dont have to be wrapped in erb tags if they are constant, but Ruby code must be wrapped in erb tags if you want the result of that code to go into your file

Can I concatenate the result of two helper methods in single erb output

I created a helper to output some text using erb
<%= helper_method %>
but if i tried to put it 2 times it will not work ..for eg..
<% =
helper_method
helper_method
%>
I am expecting the text twice...but I get only once...
Each <%= %> outputs a single string, so either join them in a single string, or do it twice.
<%= helper_method %> (or <%= ... -%> )
<%= helper_method %>
There are a variety of ways to concatenate; %Q, normal string interpolation, etc.
try something like
<%= %Q(#{helper_method} #{helper_method}) %>
<%= helper_method + helper_method %>
If you need to print a String multiple times(the string in your case comes from a helper), you can simply use the multiplier operator
#helper
def helper_method
"Text"
end
#view
<%= helper_method * 2 %>
The result in a new string like this: TextText
HTH
When you say <%= helper_method %>, = means print the output, but when you say
<% =
helper_method1
helper_method2
%>
Rails doesn't know which out put should print, because it has two methods:
helper_method1
helper_method2

Resources