简体   繁体   中英

erb if conditions with environment variables

I'm trying to do something like this for a sinatra app using erb. I've been struggling with it for quite a while and have had trouble finding good documentation for erb. Any thoughts on how to implement a working version of this would be very much appreciated.

<% if employee['filename'] == nil %>
                <img width="70" height="70" src="#{ENV['no_image_url']}">
                <% else %>
                <img width="70" height="70" data-src="#{ENV['employee_image']" src="#{ENV['employee_image']">
                end

Simply forgetting to close our your if statement.

<% if employee['filename'].nil? %>
  <img width="70" height="70" src="#{ENV['no_image_url']}">
<% else %>
  <img width="70" height="70" data-src="#{ENV['employee_image']" src="#{ENV['employee_image']">
<% end %>

This really doesn't seem like a good design though. Build the image code for rendering in your application code, and then simply render that result in your view. Then you will not have the conditional code in your view at all, you will simply render the result.

This working for me in html.erb file

 <a href="<%= ENV.fetch('variable') %>"></a> with fetch
 <a href="<%= ENV['variable'] %>"></a> with out fetch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM