Written using Phoenix 0.13.1
One common task in web apps is setting a custom page title for each URL - your homepage should have a different title than a page listing out all of your projects (for example), which should have a different title than a page showing details of a single project.
There isn’t a built-in way to do this in Phoenix, but after much reading around forums, GitHub issues, etc. I hit on two potential ways to do it.
Both involve using a custom page title call in application.html.eex
:
Both methods also require you to make the action_name/1
controller helper available to your views. You can do this in web/web.ex
by adding the action_name
method to the following line:
But the two methods differ in how I’ve defined that page_title/2
method.
Method 1 - try/apply/catch
#
This leads to a nicer interface for views, at the expense of a hacky try
/catch
block in the page_title/2
method.
In the LayoutView
, which is used by application.html.eex
, define the following methods:
This will call the page_title/2
method in the view module used by the specific page template - eg. for ProjectController.index/2
, it would use ProjectView
. It passes the page_title/2
method the name of the current action, and the assigns
data, in case you need it for generating the page title.
In the view modules, you can then define some really simple methods to set the page titles:
Alternatively, if you don’t like the pattern-matching style, you could have a single page_title/2
method and use a case
statement inside it.
I think this is a really elegant solution, that gives a really nice interface where it matters the most - in the individual view modules. The downside is, of course, that hacky try
/rescue
needed to provide a fallback page title, in case the view module doesn’t define the page_title/2
method. If there’s a nicer way to do that, I’m all ears! Please leave a comment below!
Method 2: render_existing
#
This method leverages the render_existing
method provided by Phoenix, which replicates the content_for
method familiar to those of us coming from Rails. You can use it to render templates specific to a template or controller, or as I’ve used it here, pattern-match a method call.
In the LayoutView
, you can define the following page_title/2
method:
This calls render/2
in the relevant view module, passing the first argument of “page_title”, and the second argument of the assigns
data, combined with the current action name.
In the view module, you can then pattern-match the render/2
call with the following method:
If the view module doesn’t define the render("page_title", _)
method, then Phoenix will fall back to looking for a template with the name page_title
, and if there isn’t one defined, render_existing/3
will just return nil, meaning the fallback will be used. It’s likely a more “Phoenix-y” way to do it, using Phoenix’s built-in tools, but I think the interface it provides is nowhere near as nice.
Summary#
I’m only learning both Elixir and Phoenix, and undoubtedly there will be better ways to implement what I have described above. I think I’ll be moving forward with the first solution in my practice Ticketee app, while still looking for a nicer solution to get rid of that try
/catch
clause.
Side note: Yes, I am rebuilding Ticketee from Rails 4 in Action as practice - you can see my progress here!