在我想确定我们是否在我们的开发和构建环境中的layout
文件中。haml
我们正在使用中间人。
我想做这样的事情:
- if environment == 'development'
/ Development Code
= javascript_include_tag "Dev.js"
我试图访问 Ruby 的环境变量,以及在config.rb
文件中定义一个自定义变量,但没有成功。
你几乎做对了——你需要检查一个符号而不是一个字符串:
- if environment == :development
/ Development Code
= javascript_include_tag "Dev.js"
Middleman 还添加了development?
和build?
可能更易于使用的方法:
- if development?
/ Development Code
= javascript_include_tag "Dev.js"
这也适用于 ERB:
<% if development? %>
<!-- Development Code -->
<%= javascript_include_tag "Dev.js" %>
<% end %>
First, if possible, you should separate the logic from the data. Determine your environment in your controller, and toggle the data being sent to the view (HAML layout file).
Typically you'd set a flag in your environment variables and access it in your code from ENV
. For instance, Sinatra makes the development/test/production setting available inside the code using their RACK_ENV
symbol:
:environment - configuration/deployment environment A symbol specifying the deployment environment; typically set to one of :development, :test, or :production. The :environment defaults to the value of the RACK_ENV environment variable (ENV['RACK_ENV']), or :development when no RACK_ENV environment variable is set. The environment can be set explicitly: set :environment, :production
If you have to roll your own, that's a good way to go about it.
使用中间人默认创建的 :environment 符号: http ://rubydoc.info/github/middleman/middleman/Middleman/Application#environment-instance_method
结合 haml - 您可以执行以下操作:
= javascript_include_tag "Dev.js" unless :environment == "developement"
请注意,中间人构建过程将 :environment 值更改为“构建”
你也可以使用开发?测试你是否在开发:http ://rubydoc.info/github/middleman/middleman/Middleman/Application#development%3F-instance_method
以上所有内容都适用于中间人 3.0.6,可能不适用于较小的版本(肯定不适用于 2.x)