简体   繁体   English

erb if 条件与环境变量

[英]erb if conditions with environment variables

I'm trying to do something like this for a sinatra app using erb.我正在尝试为使用 erb 的 sinatra 应用程序做类似的事情。 I've been struggling with it for quite a while and have had trouble finding good documentation for erb.我已经为此苦苦挣扎了很长一段时间,并且很难为 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语句。

<% 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这在 html.erb 文件中为我工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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