Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read config variable in Phoenix / Elixir?

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?

like image 298
Alex Craft Avatar asked Mar 01 '16 14:03

Alex Craft


2 Answers

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.

like image 149
Tiago Engel Avatar answered Sep 16 '22 22:09

Tiago Engel


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

like image 25
Sasha Fonseca Avatar answered Sep 16 '22 22:09

Sasha Fonseca