What's the more elegant and shorter way to do this in Phoenix/Elixir?
<title><%= @page_title %></title>
I can, of course, define @page_title in each action of each controller, but I want it to have a default value if I haven't defined it in an action and and haven't passed it to a template.
How can I do that? This doesn't work:
<title><%= @page_title || "my title" %></title>
If you access the variable through the assigns map with the bracket syntax, you won't get an error if the variable is not set; instead you'll get nil, which you can chain with || to provide a default value:
<title><%= assigns[:page_title] || "my title" %></title>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With