Constantinopolis allows you to set constants from your ERBed YAML file. Remarkably, your constants are available not only in ruby context, but in javascript's. It works with Rails, Sinatra, or any Ruby projects. It's inspired by settingslogic.


Add this line to your application's Gemfile:

gem 'constantinopolis'

And then execute:

$ bundle

Or install it yourself as:

$ gem install constantinopolis


Setup your Fort class

You need to create a class in your application like below.

class Istanbul < Constantinopolis::Fort # Any class name you like
  yml "#{Rails.root}/config/istanbul.yml"  # Indicate your yaml file path.
  namespace Rails.env  # Indicate top level namespace (If you want).
end!  # Don't forget this line!

If you are setting for a rails application, it's better to put this file in config/initializer


If Constantinopolis is used on rails development environment, it'll automatically be reloaded when its yaml file is updated.

To manually reload, just invoke the method reload!.

class Istanbul < Constantinopolis::Fort

# Anywhere you want to reload it

Define constants

Secondly, define constant YAML file. Of course, you can use ERB expressions in it. Top level keys are used for 'namespace' if you indicated it in your Fort class.

defaults: &defaults
  common: Common value

  <<: *defaults
  greeting: Hello, development!
  number: 1
  memorable_date: <%= %>
    located: Turkey

  <<: *defaults
  greeting: Hello, test!
  number: 2
  memorable_date: <%= + 1 %>
    located: Turkey

  <<: *defaults
  greeting: Hello, production!
  number: 3
  memorable_date: <%= + 2 %>
    located: Turkey

Access from Ruby

Congratulation! you've been already able to access these constants anywhere like below.

Istanbul.common          #=> "Common value"
Istanbul.greeting        #=> "Hello, development!"
Istanbul.number          #=> 1
Istanbul.memorable_date  #=> Wed, 23 Apr 2014      #=> Turkey

Access from Javascript

You can access your constants in javascript the same way as in ruby context. Constantinopolis provides you a simple helper method to define javascript's constants.

<%= javascript_tag Istanbul.js_code %><!-- You need to call this line before using constants. -->

<h1>Javascript context</h1>
<p id="common"></p>
<p id="greeting"></p>
<p id="number"></p>
<p id="memorable_date"></p>
<p id="is-located"></p>

<%= javascript_tag do %>
  document.getElementById("common").innerText = Istanbul.common;
  document.getElementById("greeting").innerText = Istanbul.greeting;
  document.getElementById("number").innerText = Istanbul.number;
  document.getElementById("memorable_date").innerText = Istanbul.memorable_date;
  document.getElementById("is-located").innerText =;
<% end %>

It's can be a nice to define <%= javascript_tag Istanbul.js_code %> in layouts/application.html.erb if you are developing a rails application.


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request