In this episode, we walk through the whole flow of adding a new page in our app with a dynamic page title using Twig templates.
We start by setting up a new route for the user's dashboard, create a matching controller, and then build the dashboard view file. The goal is to have a basic but working page you can visit at something like /dashboard
.
Next up, we add this new route to the navigation so it's easy to access, and then create the dashboard Twig file itself. At this point, everything works, but we notice the page title isn't updating—this is where things get interesting!
To fix the page title, we use Twig's block functionality to let each page set its own <title>
. We define a title
block inside each page template (like home.twig
and dashboard.twig
) and then modify our base layout (app.twig
) to check if a title
block is defined, and, if it is, render it in the <title>
tag along with the app name (hardcoded for now). This means each page can control what appears in the browser tab.
At the end, we point out that soon, we'll make it even nicer by pulling the app name from our config using a Twig helper, but right now, we've got dynamic page titles set up and working. If a page doesn't define a title
block, it just defaults to the app name. Nice and tidy!