Skip to content

Instantly share code, notes, and snippets.

@nicolashery
Last active January 22, 2023 15:56
Show Gist options
  • Save nicolashery/5756478 to your computer and use it in GitHub Desktop.
Save nicolashery/5756478 to your computer and use it in GitHub Desktop.
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
@0xcaff
Copy link

0xcaff commented Apr 10, 2014

@morenoh149 No. You can use CI though.

@aminalhazwani
Copy link

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
Copy link

cjwirth commented May 23, 2015

👍 This was pretty helpful.

@igolden
Copy link

igolden commented Jul 18, 2015

Useful plugin and well executed. thanks.

@yordis
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
Copy link

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

@errordeveloper
Copy link

jekyll.environment is a thing these days 👍

@k-funk
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
Copy link

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
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
Copy link

mosinski commented May 6, 2017

@k-funk nice tip thx 👍

@ScottA38
Copy link

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

Your answers are very old so sorry for the question - how exactly does Jekyll.environment allow you to add custom ENV variables in _config?

@leogzyl
Copy link

leogzyl commented Jan 22, 2023

Really helpful, thanks!

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