Reading values from the configuration file in a theme's handlebars template

Hi there! :wave::slightly_smiling_face:

I was wondering: how could I read a custom value, added in the configuration file, in one of the theme’s handlebars templates?

For example, let’s say I add a custom property named isProduction, where isProduction = NODE_ENV === "production" in the configuration file. How could I display the value of isProduction, from the configuration file, in my theme’s handlebars template?

Are there any calls to Ghost’s APIs that I could run in order to get that information? I am using Ghost v4.1.1.

Thank you so much!

I don’t think reading from the config file will ever be allowed due to security concerns. What exactly are you trying to do?

@vikaspotluri123 Thank you for your reply! :pray:

I am using Ghost in a Docker container, and my end goal is to read/display one of the passed env variables in the front-end. For example, this is how next.js does it:

I attempted to use Ghost Content API to see if I could read any env. variables, but that did not see to work either.

Any suggestions, or alternatives, would be deeply appreciated :slightly_smiling_face:

What is it you want to achieve by doing that? Reading env vars is an assumed solution to a problem, not the problem itself ;-)

@Kevin Thank you for reaching out :slightly_smiling_face:

I have different environments: development, staging, production, etc… and I have an external API url that changes based on the environment. The environment (development, staging, production…) is determined with an env. variable that I pass to our docker container during build time.

For example:

if NODE_ENV===production, the API_URL is production-api.example.org
if NODE_ENV===staging, the API_URL is staging-api.example.org
if NODE_ENV===development, the API_URL is development-api.example.org

My goal would be to update this url dynamically (API_URL) on the FE depending on the passed env. variable (NODE_ENV)

Hopefully that helps to explain my problem. Please don’t hesitate to reach out if you have more questions.

Hello.

Might not be the best solution, but I wanted to also be able to read the storage configuration at rendering time (we are using a particular image rendering service). I did that with a helper.

Checkout the folder ‘core/frontend/helpers’, there you can add some and they will be automatically registered. From this helper you can read environment variables with ‘process.env’. Then return the value you want and use the helper in the FE.

Helpers are used at rendering time, so still on the server. I don’t really see the problem of doing that. If any one has hints on security issues, would be glad to hear.

Cheers

1 Like