Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make environment variables available in Jekyll Liquid templates

Environment variables in Jekyll templates

This is one way to pass some data (API tokens, etc.) to your Jekyll templates without putting it in your _config.yml file (which is likely to be committed in your GitHub repository).

Copy the environment_variables.rb plugin to your _plugins folder, and add any environment variable you wish to have available on the site.config object.

In a Liquid template, that information will be available through the site object. For example, _layouts/default.html could contain:

<head>
   <!-- ... -->
  {% if site.env == 'production' %}
    <link rel="stylesheet" href="/build/style.min.css">
  {% else %}
    <link rel="stylesheet" href="/style.css">
  {% endif %}
</head>

Running jekyll build will use style.css by default. If you set export JEKYLL_ENV=production before running jekyll build, it will use style.min.css.

# Plugin to add environment variables to the `site` object in Liquid templates
module Jekyll
class EnvironmentVariablesGenerator < Generator
def generate(site)
site.config['env'] = ENV['JEKYLL_ENV'] || 'development'
# Add other environment variables to `site.config` here...
end
end
end
@npras

This comment has been minimized.

Copy link

npras commented Feb 17, 2014

Thanks! This was useful!

@morenoh149

This comment has been minimized.

Copy link

morenoh149 commented Mar 5, 2014

anyway to get this working on github pages? pages.github.com

@0xcaff

This comment has been minimized.

Copy link

0xcaff commented Apr 10, 2014

@morenoh149 No. You can use CI though.

@aminalhazwani

This comment has been minimized.

Copy link

aminalhazwani commented Sep 26, 2014

I understood that you write s3_id: <%= ENV['S3_ID'] %> in your s3_config.yml, but where do you store the actual id. In which file? and how?

@cjwirth

This comment has been minimized.

Copy link

cjwirth commented May 23, 2015

👍 This was pretty helpful.

@igolden

This comment has been minimized.

Copy link

igolden commented Jul 18, 2015

Useful plugin and well executed. thanks.

@yordis

This comment has been minimized.

Copy link

yordis commented Aug 17, 2015

@morenoh149 @caffinatedmonkey you can use site.github if that is defined then you are in github page. Read the first line https://help.github.com/articles/repository-metadata-on-github-pages/#available-repository-metadata

@duleorlovic

This comment has been minimized.

Copy link

duleorlovic commented Sep 30, 2015

This plugin is not needed since there exists JEKYLL_ENV
http://www.csinaction.com/2015/02/07/environments-in-jekyll-aka-jekyll_env/

@errordeveloper

This comment has been minimized.

Copy link

errordeveloper commented Oct 14, 2015

jekyll.environment is a thing these days 👍

@Dorian

This comment has been minimized.

Copy link

Dorian commented Jan 25, 2016

e.g.: {% if jekyll.environment == "development" %} .... {% endif %}

Thanks @errordeveloper

@k-funk

This comment has been minimized.

Copy link

k-funk commented Jul 29, 2016

Thanks for giving me a good start on what I was working on.

For what it's worth to other people, I wanted to include all vars in /.env, but not all environment variables loaded (which ENV) has, and didn't want to call them by name in application logic, so I used the dotenv gem and get those env vars in my code with {{ site.config.env.ENV_VAR1 }} :

# Plugin to add environment variables to the `site` object in Liquid templates
require 'dotenv'

module Jekyll
  class EnvironmentVariablesGenerator < Generator
    def generate(site)
      site.config['env'] = Dotenv.load
    end
  end
end
@crazy-max

This comment has been minimized.

Copy link

crazy-max commented Jan 11, 2017

If you want to use your conf for some plugins it's better to raise the priority to highest.
And why not pretty print the result (in debug mode) :

module Jekyll

  class EnvironmentVariablesGenerator < Generator
    priority :highest

    def generate(site)
      site.config['env'] = ENV['JEKYLL_ENV'] || 'development'
      # Add other environment variables to `site.config` here...
      Jekyll.logger.debug site.config.to_yaml
    end
  end

end
@jveillet

This comment has been minimized.

Copy link

jveillet commented Jan 24, 2017

I was just searching to do that, very helpfull thanks! 👍
As someone else poited out, I required the dotenv gem and it works wonders.

@mosinski

This comment has been minimized.

Copy link

mosinski commented May 6, 2017

@k-funk nice tip thx 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment