Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sytranvn/dfd53892383a2559cae1e9b71edc8150 to your computer and use it in GitHub Desktop.
Save sytranvn/dfd53892383a2559cae1e9b71edc8150 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment