Adding titles on your page can give users extra guidance to see what they are looking at. In Phoenix, adding this can be done with only a few changes.
At first we take the base function that we are going to use to create the
page title. This can be added to the AppNameWeb module within the
function in the
quote do ... end block. This is nice because the view
function is used in all our view modules with
use AppNameWeb, :view.
We call the function
page_title/2 and it will look like this:
Here we also see that
page_title/2 is overridable which means that modules
that use this module can implement their own
page_title/2, thus overriding the
created function and use the
page_title/2 as a backup.
This function can now be called from the layout app template and that will look like this:
In this piece of code we take the given @view_module variable, which is the view
module for the template of the current page, and call
page_title/2. The page_title
function will be given two arguments:
@view_templatewhich will be something like
assignswhich contains all the data assigned in the controller that is needed to render the page
And now the
page_title/2 can be implemented in the views, for example in a