Search code examples
rubyhamlmiddleman

How to know in what environment the code runs?


In the layout file of haml I would like to determine whether we are in our development and build environments. We're using Middleman.

I would like to do something like this:

- if environment == 'development'
    / Development Code
    = javascript_include_tag "Dev.js"

I tried to access Ruby's environment variable, as well as define a custom variable in the config.rb file with no success.


Solution

  • You’ve almost got it right – you need to check against a symbol rather than a string:

    - if environment == :development
        / Development Code
        = javascript_include_tag "Dev.js"
    

    Middleman also adds the development? and build? methods which may be easier to use:

    - if development?
        / Development Code
        = javascript_include_tag "Dev.js"
    

    This works with ERB too:

    <% if development? %>
    <!-- Development Code -->
    <%= javascript_include_tag "Dev.js" %>
    <% end %>