Widgets

One of the challenges in web development is that we have to coordinate three different client-side technologies: HTML, CSS and Javascript. Worse still, we have to place these components in different locations on the page: CSS in a style tag in the head, Javascript in a script tag before the closing body tag, and HTML in the body. And never mind if you want to put your CSS and Javascript in separate files!

In practice, this works out fairly nicely when building a single page, because we can separate our structure (HTML), style (CSS) and logic (Javascript). But when we want to build modular pieces of code that can be easily composed, it can be a headache to coordinate all three pieces separately. Widgets are Yesod’s solution to the problem. They also help with the issue of including libraries, such as jQuery, one time only.

Our four template languages- Hamlet, Cassius, Lucius and Julius- provide the raw tools for constructing your output. Widgets provide the glue that allows them to work together seamlessly.

Synopsis

  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE QuasiQuotes #-}
  3. {-# LANGUAGE TemplateHaskell #-}
  4. {-# LANGUAGE TypeFamilies #-}
  5. import Yesod
  6. data App = App
  7. mkYesod "App" [parseRoutes|
  8. / HomeR GET
  9. |]
  10. instance Yesod App
  11. getHomeR = defaultLayout $ do
  12. setTitle "My Page Title"
  13. toWidget [lucius| h1 { color: green; } |]
  14. addScriptRemote "https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"
  15. toWidget
  16. [julius|
  17. $(function() {
  18. $("h1").click(function(){
  19. alert("You clicked on the heading!");
  20. });
  21. });
  22. |]
  23. toWidgetHead
  24. [hamlet|
  25. <meta name=keywords content="some sample keywords">
  26. |]
  27. toWidget
  28. [hamlet|
  29. <h1>Here's one way of including content
  30. |]
  31. [whamlet|<h2>Here's another |]
  32. toWidgetBody
  33. [julius|
  34. alert("This is included in the body itself");
  35. |]
  36. main = warp 3000 App

This produces the following HTML (indentation added):

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>My Page Title</title>
  5. <meta name="keywords" content="some sample keywords">
  6. <style>h1{color:green}</style>
  7. </head>
  8. <body>
  9. <h1>Here's one way of including content</h1>
  10. <h2>Here's another</h2>
  11. <script>
  12. alert("This is included in the body itself");
  13. </script>
  14. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
  15. </script><script>
  16. $(function() {
  17. $('h1').click(function() {
  18. alert("You clicked on the heading!");
  19. });
  20. });
  21. </script>
  22. </body>
  23. </html>

What’s in a Widget?

At a very superficial level, an HTML document is just a bunch of nested tags. This is the approach most HTML generation tools take: you define hierarchies of tags and are done with it. But let’s imagine that I want to write a component of a page for displaying the navbar. I want this to be “plug and play”: I call the function at the right time, and the navbar is inserted at the correct point in the hierarchy.

This is where our superficial HTML generation breaks down. Our navbar likely consists of some CSS and JavaScript in addition to HTML. By the time we call the navbar function, we have already rendered the <head> tag, so it is too late to add a new <style> tag for our CSS declarations. Under normal strategies, we would need to break up our navbar function into three parts: HTML, CSS and JavaScript, and make sure that we always call all three pieces.

Widgets take a different approach. Instead of viewing an HTML document as a monolithic tree of tags, widgets see a number of distinct components in the page. In particular:

  • The title

  • External stylesheets

  • External Javascript

  • CSS declarations

  • Javascript code

  • Arbitrary <head> content

  • Arbitrary <body> content

Different components have different semantics. For example, there can only be one title, but there can be multiple external scripts and stylesheets. However, those external scripts and stylesheets should only be included once. Arbitrary head and body content, on the other hand, has no limitation (someone may want to have five lorem ipsum blocks after all).

The job of a widget is to hold onto these disparate components and apply proper logic for combining different widgets together. This consists of things like taking the last title set and ignoring others, filtering duplicates from the list of external scripts and stylesheets, and concatenating head and body content.

Constructing Widgets

In order to use widgets, you’ll obviously need to be able to get your hands on them. The most common way will be via the ToWidget typeclass, and its toWidget method. This allows you to convert your Shakespearean templates directly to a Widget: Hamlet code will appear in the body, Julius scripts inside a <script>, and Cassius and Lucius in a <style> tag.

You can actually override the default behavior and have the script and style code appear in a separate file. The scaffolded site provides this for you automatically.

But what if you want to add some <meta> tags, which need to appear in the head? Or if you want some Javascript to appear in the body instead of the head? For these purposes, Yesod provides two additional type classes: ToWidgetHead and ToWidgetBody. These work exactly as they seem they should. One example use case for this is to have fine-grained control of where your <script> tags end up getting inserted.

  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE QuasiQuotes #-}
  3. {-# LANGUAGE TemplateHaskell #-}
  4. {-# LANGUAGE TypeFamilies #-}
  5. import Yesod
  6. data App = App
  7. mkYesod "App" [parseRoutes|
  8. / HomeR GET
  9. |]
  10. instance Yesod App where
  11. getHomeR :: Handler Html
  12. getHomeR = defaultLayout $ do
  13. setTitle "toWidgetHead and toWidgetBody"
  14. toWidgetBody
  15. [hamlet|<script src=/included-in-body.js>|]
  16. toWidgetHead
  17. [hamlet|<script src=/included-in-head.js>|]
  18. main :: IO ()
  19. main = warp 3000 App

Note that even though toWidgetHead was called after toWidgetBody, the latter <script> tag appears first in the generated HTML.

In addition, there are a number of other functions for creating specific kinds of Widgets:

setTitle

Turns some HTML into the page title.

toWidgetMedia

Works the same as toWidget, but takes an additional parameter to indicate what kind of media this applies to. Useful for creating print stylesheets, for instance.

addStylesheet

Adds a reference, via a <link> tag, to an external stylesheet. Takes a type-safe URL.

addStylesheetRemote

Same as addStylesheet, but takes a normal URL. Useful for referring to files hosted on a CDN, like Google’s jQuery UI CSS files.

addScript

Adds a reference, via a <script> tag, to an external script. Takes a type-safe URL.

addScriptRemote

Same as addScript, but takes a normal URL. Useful for referring to files hosted on a CDN, like Google’s jQuery.

Combining Widgets

The whole idea of widgets is to increase composability. You can take these individual pieces of HTML, CSS and Javascript, combine them together into something more complicated, and then combine these larger entities into complete pages. This all works naturally through the Monad instance of Widget, meaning you can use do-notation to compose pieces together.

  1. myWidget1 = do
  2. toWidget [hamlet|<h1>My Title|]
  3. toWidget [lucius|h1 { color: green } |]
  4. myWidget2 = do
  5. setTitle "My Page Title"
  6. addScriptRemote "http://www.example.com/script.js"
  7. myWidget = do
  8. myWidget1
  9. myWidget2
  10. -- or, if you want
  11. myWidget' = myWidget1 >> myWidget2

If you’re so inclined, there’s also a Monoid instance of Widget, meaning you can use mconcat or a Writer monad to build things up. In my experience, it’s easiest and most natural to just use do-notation.

Generate IDs

If we’re really going for true code reuse here, we’re eventually going to run into name conflicts. Let’s say that there are two helper libraries that both use the class name “foo” to affect styling. We want to avoid such a possibility. Therefore, we have the newIdent function. This function automatically generates a word that is unique for this handler.

  1. getRootR = defaultLayout $ do
  2. headerClass <- newIdent
  3. toWidget [hamlet|<h1 .#{headerClass}>My Header|]
  4. toWidget [lucius| .#{headerClass} { color: green; } |]

whamlet

Let’s say you’ve got a fairly standard Hamlet template, that embeds another Hamlet template to represent the footer:

  1. page =
  2. [hamlet|
  3. <p>This is my page. I hope you enjoyed it.
  4. ^{footer}
  5. |]
  6. footer =
  7. [hamlet|
  8. <footer>
  9. <p>That's all folks!
  10. |]

That works fine if the footer is plain old HTML, but what if we want to add some style? Well, we can easily spice up the footer by turning it into a Widget:

  1. footer = do
  2. toWidget
  3. [lucius|
  4. footer {
  5. font-weight: bold;
  6. text-align: center
  7. }
  8. |]
  9. toWidget
  10. [hamlet|
  11. <footer>
  12. <p>That's all folks!
  13. |]

But now we’ve got a problem: a Hamlet template can only embed another Hamlet template; it knows nothing about a Widget. This is where whamlet comes in. It takes exactly the same syntax as normal Hamlet, and variable (#{…}) and URL (@{…}) interpolation are unchanged. But embedding (^{…}) takes a Widget, and the final result is a Widget. To use it, we can just do:

  1. page =
  2. [whamlet|
  3. <p>This is my page. I hope you enjoyed it.
  4. ^{footer}
  5. |]

There is also whamletFile, if you would prefer to keep your template in a separate file.

The scaffolded site has an even more convenient function, widgetFile, which will also include your Lucius, Cassius, and Julius files automatically. We’ll cover that in the scaffolding chapter.

Types

You may have noticed that I’ve been avoiding type signatures so far. The simple answer is that each widget is a value of type Widget. But if you look through the Yesod libraries, you’ll find no definition of the Widget type. What gives?

Yesod defines a very similar type: data WidgetT site m a. This data type is a monad transformer. The last two arguments are the underlying monad and the monadic value, respectively. The site parameter is the specific foundation type for your individual application. Since this type varies for each and every site, it’s impossible for the libraries to define a single Widget datatype which would work for every application.

Instead, the mkYesod Template Haskell function generates this type synonym for you. Assuming your foundation data type is called MyApp, your Widget synonym is defined as:

  1. type Widget = WidgetT MyApp IO ()

We set the monadic value to be (), since a widget’s value will ultimately be thrown away. IO is the standard base monad, and will be used in almost all cases. The only exception is when writing a subsite. Subsites are a more advanced topic, and will be covered later in their own chapter.

Once we know about our Widget type synonym, it’s easy to add signatures to our previous code samples:

  1. footer :: Widget
  2. footer = do
  3. toWidget
  4. [lucius|
  5. footer {
  6. font-weight: bold;
  7. text-align: center
  8. }
  9. |]
  10. toWidget
  11. [hamlet|
  12. <footer>
  13. <p>That's all folks!
  14. |]
  15. page :: Widget
  16. page =
  17. [whamlet|
  18. <p>This is my page. I hope you enjoyed it.
  19. ^{footer}
  20. |]

When we start digging into handler functions some more, we’ll encounter a similar situation with the HandlerT and Handler types.

Using Widgets

It’s all well and good that we have these beautiful Widget datatypes, but how exactly do we turn them into something the user can interact with? The most commonly used function is defaultLayout, which essentially has the type signature Widget → Handler Html.

defaultLayout is actually a typeclass method, which can be overridden for each application. This is how Yesod apps are themed. So we’re still left with the question: when we’re inside defaultLayout, how do we unwrap a Widget? The answer is widgetToPageContent. Let’s look at some (simplified) types:

  1. data PageContent url = PageContent
  2. { pageTitle :: Html
  3. , pageHead :: HtmlUrl url
  4. , pageBody :: HtmlUrl url
  5. }
  6. widgetToPageContent :: Widget -> Handler (PageContent url)

This is getting closer to what we need. We now have direct access to the HTML making up the head and body, as well as the title. At this point, we can use Hamlet to combine them all together into a single document, along with our site layout, and we use withUrlRenderer to convert that Hamlet result into actual HTML that’s ready to be shown to the user. The next example demonstrates this process.

  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE QuasiQuotes #-}
  3. {-# LANGUAGE TemplateHaskell #-}
  4. {-# LANGUAGE TypeFamilies #-}
  5. import Yesod
  6. data App = App
  7. mkYesod "App" [parseRoutes|
  8. / HomeR GET
  9. |]
  10. myLayout :: Widget -> Handler Html
  11. myLayout widget = do
  12. pc <- widgetToPageContent widget
  13. withUrlRenderer
  14. [hamlet|
  15. $doctype 5
  16. <html>
  17. <head>
  18. <title>#{pageTitle pc}
  19. <meta charset=utf-8>
  20. <style>body { font-family: verdana }
  21. ^{pageHead pc}
  22. <body>
  23. <article>
  24. ^{pageBody pc}
  25. |]
  26. instance Yesod App where
  27. defaultLayout = myLayout
  28. getHomeR :: Handler Html
  29. getHomeR = defaultLayout
  30. [whamlet|
  31. <p>Hello World!
  32. |]
  33. main :: IO ()
  34. main = warp 3000 App

There’s still one thing that bothers me: that style tag. There are a few problems with it:

  • Unlike Lucius or Cassius, it doesn’t get compile-time checked for correctness.

  • Granted that the current example is very simple, but in something more complicated we could get into character escaping issues.

  • We’ll now have two style tags instead of one: the one produced by myLayout, and the one generated in the pageHead based on the styles set in the widget.

We have one more trick in our bag to address this: we apply some last-minute adjustments to the widget itself before calling widgetToPageContent. It’s actually very easy to do: we just use do-notation again.

  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE QuasiQuotes #-}
  3. {-# LANGUAGE TemplateHaskell #-}
  4. {-# LANGUAGE TypeFamilies #-}
  5. import Yesod
  6. data App = App
  7. mkYesod "App" [parseRoutes|
  8. / HomeR GET
  9. |]
  10. myLayout :: Widget -> Handler Html
  11. myLayout widget = do
  12. pc <- widgetToPageContent $ do
  13. widget
  14. toWidget [lucius| body { font-family: verdana } |]
  15. withUrlRenderer
  16. [hamlet|
  17. $doctype 5
  18. <html>
  19. <head>
  20. <title>#{pageTitle pc}
  21. <meta charset=utf-8>
  22. ^{pageHead pc}
  23. <body>
  24. <article>
  25. ^{pageBody pc}
  26. |]
  27. instance Yesod App where
  28. defaultLayout = myLayout
  29. getHomeR :: Handler Html
  30. getHomeR = defaultLayout
  31. [whamlet|
  32. <p>Hello World!
  33. |]
  34. main :: IO ()
  35. main = warp 3000 App

Using handler functions

We haven’t covered too much of the handler functionality yet, but once we do, the question arises: how do we use those functions in a widget? For example, what if your widget needs to look up a query string parameter using lookupGetParam?

The first answer is the function handlerToWidget, which can convert a Handler action into a Widget answer. However, in many cases, this won’t be necessary. Consider the type signature of lookupGetParam:

  1. lookupGetParam :: MonadHandler m => Text -> m (Maybe Text)

This function will live in any instance of MonadHandler. And conveniently, Widget is also a MonadHandler instance. This means that most code can be run in either Handler or Widget. And if you need to explicitly convert from Handler to Widget, you can always use handlerToWidget.

This is a significant departure from how Yesod worked in versions 1.1 and earlier. Previously, there was no MonadHandler typeclass, and all functions needed to be explicitly converted using lift, not handlerToWidget. The new system is not only easier to use, but also avoids any strange monad transformer tricks which were previously employed.

Summary

The basic building block of each page is a widget. Individual snippets of HTML, CSS, and Javascript can be turned into widgets via the polymorphic toWidget function. Using do-notation, you can combine these individual widgets into larger widgets, eventually containing all the content of your page.

Unwrapping these widgets is usually performed within the defaultLayout function, which can be used to apply a unified look-and-feel to all your pages.