Is it possible to use a variable which is passed from controller to view in render parameters to be used in javascript and how to do it?
2 Answers
Another way you can use PhoenixGon it generates script tag with variables and additional methods for simplicity. It generate all stuff for you. You only need to use it from window.Gon or wundow.YouApplicationNamespace. And you don't need other rendering and data attributing in html.
In controller:
def index(conn, _params) do
conn = put_gon(conn, :variable, :value)
render conn, "index.html"
end
In js module or browser console:
window.Gon.getAsset('variable')
# => 'value'
It also keeps Mix.env for using in js.