I'd like to add logic into web/templates/user/form.html.eex which gets rendered in user#new
and user#edit
depending on @current_user
. But if I do the following code I get an assign @current_user not available in eex template
error.
<%= if @current_user do %>
<h1>Example</h1>
<% end %>
How can I make @current_user
available in the form.html.eex
?
Assuming you are rendering form.html.eex
using something like:
render("form.html.eex", changeset: @changeset, conn: @conn)
And you are rendering the "parent" template with @current_user
available in assigns.
You can pass @current_user
in the render
function in your template:
render("form.html.eex", changeset: @changeset, conn: @conn, current_user: @current_user)
You can pass everything in assigns with:
render("form.html.eex", @conn.assigns)
However you should do this with caution, remember explicit is better than implicit.
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