Question

I'm working on a user registration process in Yesod. The plan is to have send a newly registered user an email which activates their account. I have attempted to create a widget which included a Typed-Url back to the route that accepts the email confirmation code.

---template.hamlet

<h3>Email Change Confirmation
<p>A new email address has been set for your account with Dropship Center.
    $case ecrType
        $of New
            <p>Please click
            <a href=@{UserConfirmationR}/#{code}>here
            to confirm.

---User.hs

import           Text.Blaze.Html.Renderer.Text (renderHtml)

postUserR :: Handler Value
postUserR = do
    val <- parseJsonBody :: Handler (Result Registration)
    case val of
        Error e -> return $ object [ "error" .= e ]
        Success Registration{..} -> do

        ...database actions...

        let ecrType = New
            code    = (ecrNewConfirm ecr)
            html    = renderHtml $ $(widgetFile "ecr-message")

        ...send confirmation email using "html" as the message body...

Unfortunately I keep getting the following error. Which I haven't been able to figure out.

Handler/User.hs:84:46:
Couldn't match type `WidgetT site0 m0'
              with `blaze-markup-0.6.0.0:Text.Blaze.Internal.MarkupM'
Expected type: blaze-markup-0.6.0.0:Text.Blaze.Internal.MarkupM ()
  Actual type: WidgetT site0 m0 ()
In the return type of a call of `asWidgetT . toWidget'
In a stmt of a 'do' block:
  (asWidgetT . toWidget)
    ((blaze-markup-0.6.0.0:Text.Blaze.Internal.preEscapedText . pack)
       "<h3>Email Change Confirmation</h3>\
       \<p>A new email address has been set for your account with Dropship Center.")
In a stmt of a 'do' block:
  do { (asWidgetT . toWidget)
         ((blaze-markup-0.6.0.0:Text.Blaze.Internal.preEscapedText . pack)
            "<h3>Email Change Confirmation</h3>\
            \<p>A new email address has been set for your account with Dropship Center.");
       case ecrType of {
         New -> do { ... }
         Old -> do { ... } };
       (asWidgetT . toWidget)
         ((blaze-markup-0.6.0.0:Text.Blaze.Internal.preEscapedText . pack)
            "</p>") }

Is there some middle stage that converts a Widget into an Html type?

Was it helpful?

Solution

In order to unpack a Widget, you need to use widgetToPageContent. However, that's probably not what you want, since I doubt you intend to have CSS and Javascript in the email. Instead, you probably want to use hamletFile, which can generate just HTML. You'll need to pass in a URL rendering function, which you can get with getUrlRenderParams. The incantation will look something like:

renderer <- getUrlRenderer
let html = $(hamletFile "filepath.hamlet") renderer
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top