Phoenix offers the
render_existing/3 function, which tries to render the template for the provided module, or returns
In your existing phoenix project open up your
MyApp.LayoutView usually located in
views/layout_view.ex and add the following.
Now open the view module of any of your controller actions, for example the
session_view.ex. Lets say you want the title for your login action to read “Log in to MyApp”.
new maps to the current view template
new, it could also be
show or any other value.
Finally, open up your main layout, usually
templates/layout/app.html.eex and add the following to the
The above line will call the
title/3 function in your
layout_view.ex defined at the beginning of this post, which will turn use
render_existing/3 on the current view module (in our example the
session_view.ex) for the template
For any page you don’t define a
title.<action>.html the call will just silently fail and return the default value instead.
render function to your view, and update the layout to call the function.
I heavily use this for custom titles, breadcrumbs and meta tags. Here is how my
This post was updated on 22. December 2018 to use a better approach using