60

I'd like to set the title of my app in my/config/config.exs file:

config :my, My.Endpoint,
  url: [host: "localhost"],
  root: Path.dirname(__DIR__),
  secret_key_base: "secret",
  title: "My App"

How can I read title later to use it in template? Something like:

<div>
  Title of my app is <%= ??? %>
</div>

I tried conn.title and it says there's no such key. But, when I try conn.secret_key_base it works. Why?

5 Answers 5

80

The get_env function is part of the Application module from the Elixir/Erlang core.

This function returns the value for a specific key in the app's environment. Considering your configuration, you would access the title property this way:

Application.get_env(:my, My.Endpoint)[:title]

The third parameter is for passing a default value when the config key doesn't exist.

42

You may use Application.get_env(:my, :title)

3
  • You must have the dev.exs, prod.exs and test.exs files to get the current environment configurations. Commented Mar 1, 2016 at 15:18
  • 9
    This answer is incorrect, at least on my system. tiagohngl gives the proper incantation. Commented Nov 11, 2016 at 1:02
  • app parameter is :my, and key is My.Endpoint. So this answer is incorrect. The one by @tiago-herique-engel should be checked
    – febeling
    Commented Mar 15, 2018 at 16:23
20

Let's say in dev.ex file you have a config variables

config :app_name, AppName.Endpoint,
  api_prefix: "api/v2",
  api_host: "http://0.0.0.0",
  api_port: "3000"

You can fetch all three config variables

Application.get_env(:app_name, AppName.Endpoint)[:api_prefix]
Application.get_env(:app_name, AppName.Endpoint)[:api_host]
Application.get_env(:app_name, AppName.Endpoint)[:api_port] 
6

To have clear separation between my custom configuration and configuration for phoenix and other modules I used:

config :ace, :config,
  root: Path.dirname(__DIR__),
  title: "Ace"

And then you retrieve the value using a get_env call like:

iex> Application.get_env(:ace, :config)[:title]
"Ace" 
1

if you look under the hood how the config is just a function that add config values to a keyword list and later you can access them in your app

config/2 takes a key and maps it to keyword_list, config/3 takes a key and adds key with keyword_list as value.

Since you are using config/3 it namesapces your config under My.Endpoint this would work Application.get_env(:my, My.Endpoint, :title)

2
  • config/2 adds multiple key-value pairs in one call, while config/3 add only one pair. There's always a as first app parameter. Application.get_env always fetches one value for the given key and in the given application. The third parameter specifies a default value, in case the the app or key are missing.
    – febeling
    Commented Mar 15, 2018 at 16:31
  • Using Application.get_env(:my, My.Endpoint, :title) is just plain wrong. My.Endpoint always has values, and :title isn't an appropriate default (clarifying febeling's comment)
    – iheggie
    Commented Apr 8, 2018 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.