Why this project?
TL;DR You can use environment variables in your Lektor templates.
I’ve been working with Lektor as as static site generator in quite a few projects and really enjoy it. Most recently I work on a project that used an environment variable to create slightly different version of the site for development, staging and production.
Lektor doesn’t have a way to add environment variables into the templates, so I started building my own little plugin.
How to install it in Lektor
You can easily install this plugin following the Lektor docs. All you need to do is run:
$ lektor plugin add lektor-envvars
This will automatically install the plugin and add it to your project configuration.
Using environment variables
You are able to access environment variables using the envvars function inside your Jinja2 template. This function is added whenever lektor is running a new build.
All environment variables are prefixed with LEKTOR_ by default. Let’s look at a simple example with an environment varialbe LEKTOR_DEBUG=true:
$ export LEKTOR_DEBUG=true
You can access this variable inside any Jinja2 template:
{{ envvars('DEBUG') }}
which will display true instead.
Converting values
That’s a great start but what if you want this to be a boolean value instead of the string true? You simply convert the value:
{{ envvars('DEBUG', bool) }}
or you can now even do:
{% if envvars('DEBUG', bool) %}
...
{% endif %}
Custom prefixes (or no prefix)
If you don’t like the LEKTOR_ prefix, you can either use your own prefix by setting the prefix in the configs/lektor-envvars.ini file:
[envvars] prefix = MY_OWN_
You can now use MY_OWN_DEBUG instead of LEKTOR_DEBUG. This means that all environment variables need to be prefixed with MY_OWN_ now instead.
You can also ignore the prefix all together:
{{ envvars('DEBUG', no_prefix=True) }}
which will give you access to the environment variable DEBUG.
License
This code is licensed under the MIT License.
Release files for lektor-envvars 18.6.12.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lektor-envvars-18.6.12.4.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lektor_envvars-18.6.12.4-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 6.8 kB
Release files / lektor-envvars-18.6.12.4.tar.gz
| Download URL | lektor-envvars-18.6.12.4.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1c22f06495d51256d8a3365c4a22b39f9dabda2ac4adac9558f11ff688d6245d
|
|
BLAKE2b-256 checksum How to use checksums |
9bef864f0a19332ee808c22435ee3d40b9f79ebdf1f71e91a5f5f15f437ce539
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / lektor_envvars-18.6.12.4-py2.py3-none-any.whl
| Download URL | lektor_envvars-18.6.12.4-py2.py3-none-any.whl |
|---|---|
| Size | 3.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
8c2de6df4277347f5b848fe0129643474e54930187142af595327c3c603333a2
|
|
BLAKE2b-256 checksum How to use checksums |
05e3cac22583b8fedbedb620bdf26b15b8ecdac97d194ad76fc6b5e24dab2304
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |