middlemanのtemplate,layoutの内で,環境を判定する

middleman(ruby製の静的サイトジェネレータ)では,開発環境を設定できる。

環境については,middlemanのドキュメントの開発環境と configure ブロックの変更を参照。

bundle exec middleman -e "環境名"

enviroments/以下に,環境名.rbファイルを作り,そこに環境ごとのconfigを書くことも可能。

たとえば,environments/test.rbファイルを作ると,testという環境ができる。

test環境でserverを立ち上げるには,

bundle exec middleman -e test

test環境でbuildするには,

bundle exec middleman build -e test

コマンドで環境を指定しない場合の環境は,serve -> development,build -> productionになる。

templateやlayoutの中で,特定の環境であるかを調べるには,environment? を使う。environment?の引数に,環境名のシンボルを入れる。

例えば,

<% if environment?(:production) %>
  <p>環境はproduction</p>
<% end %>

とすると,production環境(build時か,-e productionを指定した時)には,<p>…</p>が出力される。