Problem
You want to set the <title>
of the page within a Handler function.
Solution
Yesod provides two functions which can be called inside a handler which are setTitle
and setTitleI
. Lets assume you already use the default layout:
{-# OverloadedStrings #-}
defaultLayout $ do
-- Set the title with a simple Text
setTitle "This is my new title"
$(widgetFile "index")
To set a translated title we can use the setTitleI
function:
defaultLayout $ do
-- Set the title with a translated message
setTitleI MsgMyTitle
$(widgetFile "index")
Discussion
The setTitle function is aware of all of your calls to it. Hence, if you call it multiple times, the last call will override the calls which were made before. With the help of setTitleI
we can set <title>
tags which accept a translation message.
Since setTitle
is expecting a Text
type we need to use OverloadedStrings
when using this function in our module.