I have two template folder in my web/templates folder:
> ls web/templates
personal_info user
What I want is to render some template from user
folder in another view of personal_info
. so I have a file at path: web/templates/personal_info/index.html.eex
, I have following content:
<%= render "user/xyz.html" %>
But I get following error:
[error] #PID<0.821.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /
** (exit) an exception was raised:
** (Phoenix.Template.UndefinedError) Could not render "user/xyz.html" for MyApp.PersonalInfoView, please define a matching clause for render/1 or define a template at "web/templates/personal_info". The following templates were compiled:
* index.html
Please tell me how do I render template defined in other folder, I tried few permutations, but none worked.
Phoenix templates are just functions, so when you want to render your UserView
's "xyz.html" template from your PersonalInfo
's view, you just call the function!
Let's say you are inside the web/templates/personal_info/show.html.eex
template. (Phoenix.View.render
is already imported for you):
<%= render UserView, "xyz.html", user: user %>
Of if you want to pass along all the template assigns that your PersonalInfo template was provided:
<%= render UserView, "xyz.html", assigns %>
As you found, this works from anywhere, because templates are Just Functions. For example, the same thing would work in iex:
iex> Phoenix.View.render(MyApp.UserView, "xyz.html")
"<h1>User ..."