sheldonkreger
sheldonkreger

Reputation: 938

Load environment variable in template file in Phoenix Framework

In a controller, I can load environment variables defined in config.exs using Application.get_env/3.

Is it possible to do load an environment variable directly inside a template file, aside from passing it through a controller? In particular, I'd like to load it in one of the layout files.

Upvotes: 3

Views: 950

Answers (1)

Chase
Chase

Reputation: 2826

This q/a from 2015 will be helpful. As Dogber said, Application.get_env/3 will do exactly what you want even in a template. Take a look at the template docs. Variables take the form:

<p><%= @key %></p>

So, you can do the following:

<div class="jumbotron">
  <h3>This is h3 text</h3>

  <%= Application.get_env(:sheldonkreger_app, :some_env_var) %>
</div>

Phoenix uses Elixir EEx templates so the above gets built at compile time and is safe and ready to use in your app.

Upvotes: 3

Related Questions