Shakespearean Templates

Yesod uses the Shakespearean family of template languages as its standard approach to HTML, CSS and Javascript creation. This language family shares some common syntax, as well as overarching principles:

  • As little interference to the underlying language as possible, while providing conveniences where unobtrusive.

  • Compile-time guarantees on well-formed content.

  • Static type safety, greatly helping the prevention of XSS (cross-site scripting) attacks.

  • Automatic validation of interpolated links, whenever possible, through type-safe URLs.

There is nothing inherently tying Yesod to these languages, or the other way around: each can be used independently of the other. This chapter will address these template languages on their own, while the remainder of the book will use them to enhance Yesod application development.

Synopsis

There are four main languages at play: Hamlet is an HTML templating language, Julius is for Javascript, and Cassius and Lucius are both for CSS. Hamlet and Cassius are both whitespace-sensitive formats, using indentation to denote nesting. By contrast, Lucius is a superset of CSS, keeping CSS’s braces for denoting nesting. Julius is a simple passthrough language for producing Javascript; the only added feature is variable interpolation.

Cassius is in fact just an alternate syntax for Lucius. They both use the same processing engine underneath, but Cassius files have indentation converted into braces before processing. The choice between the two is purely one of syntactical preference.

Hamlet (HTML)

  1. $doctype 5
  2. <html>
  3. <head>
  4. <title>#{pageTitle} - My Site
  5. <link rel=stylesheet href=@{Stylesheet}>
  6. <body>
  7. <h1 .page-title>#{pageTitle}
  8. <p>Here is a list of your friends:
  9. $if null friends
  10. <p>Sorry, I lied, you don't have any friends.
  11. $else
  12. <ul>
  13. $forall Friend name age <- friends
  14. <li>#{name} (#{age} years old)
  15. <footer>^{copyright}

Lucius (CSS)

  1. section.blog {
  2. padding: 1em;
  3. border: 1px solid #000;
  4. h1 {
  5. color: #{headingColor};
  6. background-image: url(@{MyBackgroundR});
  7. }
  8. }

Cassius (CSS)

The following is equivalent to the Lucius example above.

  1. section.blog
  2. padding: 1em
  3. border: 1px solid #000
  4. h1
  5. color: #{headingColor}
  6. background-image: url(@{MyBackgroundR})

Julius (Javascript)

String interpolation works slightly differently in Julius. This is important from a security standpoint — all interpolated values are valid JSON by default which helps prevent XSS attacks. You’ll need to use the rawJS function to get a string that isn’t wrapped in double-quotes.

Be sure not to use rawJS with any input you don’t trust, e.g., anything submitted by a user.

  1. -- import the rawJS function by importing its typeclass
  2. import Text.Julius (RawJS (..))
  1. $(function(){
  2. $("section.#{rawJS sectionClass}").hide();
  3. $("#mybutton").click(function(){document.location = "@{SomeRouteR}";});
  4. ^{addBling}
  5. });

Types

Before we jump into syntax, let’s take a look at the various types involved. We mentioned in the introduction that types help protect us from XSS attacks. For example, let’s say that we have an HTML template that should display someone’s name. It might look like this:

  1. <p>Hello, my name is #{name}

#{…} is how we do variable interpolation in Shakespeare.

What should happen to name, and what should its datatype be? A naive approach would be to use a Text value, and insert it verbatim. But that would give us quite a problem when name is equal to something like:

  1. <script src='http://nefarious.com/evil.js'></script>

What we want is to be able to entity-encode the name, so that < becomes &lt;.

An equally naive approach is to simply entity-encode every piece of text that gets embedded. What happens when you have some preexisting HTML generated from another process? For example, on the Yesod website, all Haskell code snippets are run through a colorizing function that wraps up words in appropriate span tags. If we entity escaped everything, code snippets would be completely unreadable!

Instead, we have an Html datatype. In order to generate an Html value, we have two options for APIs: the ToMarkup typeclass provides a way to convert String and Text values into Html, via its toHtml function, automatically escaping entities along the way. This would be the approach we’d want for the name above. For the code snippet example, we would use the preEscapedToMarkup function.

When you use variable interpolation in Hamlet (the HTML Shakespeare language), it automatically applies a toHtml call to the value inside. So if you interpolate a String, it will be entity-escaped. But if you provide an Html value, it will appear unmodified. In the code snippet example, we might interpolate with something like #{preEscapedToMarkup myHaskellHtml}.

The Html datatype, as well as the functions mentioned, are all provided by the blaze-html package. This allows Hamlet to interact with all other blaze-html packages, and lets Hamlet provide a general solution for producing blaze-html values. Also, we get to take advantage of blaze-html’s amazing performance.

Similarly, we have Css/ToCss, as well as Javascript/ToJavascript. These provide some compile-time sanity checks that we haven’t accidentally stuck some HTML in our CSS.

One other advantage on the CSS side is some helper datatypes for colors and units. For example:

  1. .red { color: #{colorRed} }

Please see the Haddock documentation for more details.

Type-safe URLs

Possibly the most unique feature in Yesod is type-safe URLs, and the ability to use them conveniently is provided directly by Shakespeare. Usage is nearly identical to variable interpolation; we just use the at-sign (@) instead of the hash (#). We’ll cover the syntax later; first, let’s clarify the intuition.

Suppose we have an application with two routes: http://example.com/profile/home is the homepage, and http://example.com/display/time displays the current time. And let’s say we want to link from the homepage to the time. I can think of three different ways of constructing the URL:

  1. As a relative link: ../display/time

  2. As an absolute link, without a domain: /display/time

  3. As an absolute link, with a domain: http://example.com/display/time

There are problems with each approach: the first will break if either URL changes. Also, it’s not suitable for all use cases; RSS and Atom feeds, for instance, require absolute URLs. The second is more resilient to change than the first, but still won’t be acceptable for RSS and Atom. And while the third works fine for all use cases, you’ll need to update every single URL in your application whenever your domain name changes. You think that doesn’t happen often? Just wait till you move from your development to staging and finally production server.

But more importantly, there is one huge problem with all approaches: if you change your routes at all, the compiler won’t warn you about the broken links. Not to mention that typos can wreak havoc as well.

The goal of type-safe URLs is to let the compiler check things for us as much as possible. In order to facilitate this, our first step must be to move away from plain old text, which the compiler doesn’t understand, to some well defined datatypes. For our simple application, let’s model our routes with a sum type:

  1. data MyRoute = Home | Time

Instead of placing a link like /display/time in our template, we can use the Time constructor. But at the end of the day, HTML is made up of text, not data types, so we need some way to convert these values to text. We call this a URL rendering function, and a simple one is:

  1. renderMyRoute :: MyRoute -> Text
  2. renderMyRoute Home = "http://example.com/profile/home"
  3. renderMyRoute Time = "http://example.com/display/time"

URL rendering functions are actually a bit more complicated than this. They need to address query string parameters, handle records within the constructor, and more intelligently handle the domain name. But in practice, you don’t need to worry about this, since Yesod will automatically create your render functions. The one thing to point out is that the type signature is actually a little more complicated to handle query strings:

  1. type Query = [(Text, Text)]
  2. type Render url = url -> Query -> Text
  3. renderMyRoute :: Render MyRoute
  4. renderMyRoute Home _ = ...
  5. renderMyRoute Time _ = ...

OK, we have our render function, and we have type-safe URLs embedded in the templates. How does this fit together exactly? Instead of generating an Html (or Css or Javascript) value directly, Shakespearean templates actually produce a function, which takes this render function and produces HTML. To see this better, let’s have a quick (fake) peek at how Hamlet would work under the surface. Supposing we had a template:

  1. <a href=@{Time}>The time

this would translate roughly into the Haskell code:

  1. \render -> mconcat ["<a href='", render Time, "'>The time</a>"]

Syntax

All Shakespearean languages share the same interpolation syntax, and are able to utilize type-safe URLs. They differ in the syntax specific for their target language (HTML, CSS, or Javascript). Let’s explore each language in turn.

Hamlet Syntax

Hamlet is the most sophisticated of the languages. Not only does it provide syntax for generating HTML, it also allows for basic control structures: conditionals, looping, and maybes.

Tags

Obviously tags will play an important part of any HTML template language. In Hamlet, we try to stick very close to existing HTML syntax to make the language more comfortable. However, instead of using closing tags to denote nesting, we use indentation. So something like this in HTML:

  1. <body>
  2. <p>Some paragraph.</p>
  3. <ul>
  4. <li>Item 1</li>
  5. <li>Item 2</li>
  6. </ul>
  7. </body>

would be

  1. <body>
  2. <p>Some paragraph.
  3. <ul>
  4. <li>Item 1
  5. <li>Item 2

In general, we find this to be easier to follow than HTML once you get accustomed to it. The only tricky part comes with dealing with whitespace before and after tags. For example, let’s say you want to create the HTML

  1. <p>Paragraph <i>italic</i> end.</p>

We want to make sure that whitespace is preserved after the word “Paragraph” and before the word “end”. To do so, we use two simple escape characters:

  1. <p>
  2. Paragraph #
  3. <i>italic
  4. \ end.

The whitespace escape rules are actually quite simple:

  1. If the first non-space character in a line is a backslash, the backslash is ignored. (Note: this will also cause any tag on this line to be treated as plain text.)

  2. If the last character in a line is a hash, it is ignored.

One other thing. Hamlet does not escape entities within its content. This is done on purpose to allow existing HTML to be more easily copied in. So the example above could also be written as:

  1. <p>Paragraph <i>italic</i> end.

Notice that the first tag will be automatically closed by Hamlet, while the inner “i” tag will not. You are free to use whichever approach you want, there is no penalty for either choice. Be aware, however, that the only time you use closing tags in Hamlet is for such inline tags; normal tags are not closed.

Another outcome of this is that any tags after the first tag do not have special treatment for IDs and classes. For example, the Hamlet snippet:

  1. <p #firstid>Paragraph <i #secondid>italic</i> end.

generates the HTML:

  1. <p id="firstid">Paragraph <i #secondid>italic</i> end.</p>

Notice how the p tag is automatically closed, and its attributes get special treatment, whereas the i tag is treated as plain text.

Interpolation

What we have so far is a nice, simplified HTML, but it doesn’t let us interact with our Haskell code at all. How do we pass in variables? Simple: with interpolation:

  1. <head>
  2. <title>#{title}

The hash followed by a pair of braces denotes variable interpolation. In the case above, the title variable from the scope in which the template was called will be used. Let me state that again: Hamlet automatically has access to the variables in scope when it’s called. There is no need to specifically pass variables in.

You can apply functions within an interpolation. You can use string and numeric literals in an interpolation. You can use qualified modules. Both parentheses and the dollar sign can be used to group statements together. And at the end, the toHtml function is applied to the result, meaning any instance of ToMarkup can be interpolated. Take, for instance, the following code.

  1. -- Just ignore the quasiquote stuff for now, and that shamlet thing.
  2. -- It will be explained later.
  3. {-# LANGUAGE QuasiQuotes #-}
  4. import Text.Hamlet (shamlet)
  5. import Text.Blaze.Html.Renderer.String (renderHtml)
  6. import Data.Char (toLower)
  7. import Data.List (sort)
  8. data Person = Person
  9. { name :: String
  10. , age :: Int
  11. }
  12. main :: IO ()
  13. main = putStrLn $ renderHtml [shamlet|
  14. <p>Hello, my name is #{name person} and I am #{show $ age person}.
  15. <p>
  16. Let's do some funny stuff with my name: #
  17. <b>#{sort $ map toLower (name person)}
  18. <p>Oh, and in 5 years I'll be #{show ((+) 5 (age person))} years old.
  19. |]
  20. where
  21. person = Person "Michael" 26

What about our much-touted type-safe URLs? They are almost identical to variable interpolation in every way, except they start with an at-sign (@) instead. In addition, there is embedding via a caret (^) which allows you to embed another template of the same type. The next code sample demonstrates both of these.

  1. {-# LANGUAGE QuasiQuotes #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. import Text.Hamlet (HtmlUrl, hamlet)
  4. import Text.Blaze.Html.Renderer.String (renderHtml)
  5. import Data.Text (Text)
  6. data MyRoute = Home
  7. render :: MyRoute -> [(Text, Text)] -> Text
  8. render Home _ = "/home"
  9. footer :: HtmlUrl MyRoute
  10. footer = [hamlet|
  11. <footer>
  12. Return to #
  13. <a href=@{Home}>Homepage
  14. .
  15. |]
  16. main :: IO ()
  17. main = putStrLn $ renderHtml $ [hamlet|
  18. <body>
  19. <p>This is my page.
  20. ^{footer}
  21. |] render

Additionally, there is a variant of URL interpolation which allows you to embed query string parameters. This can be useful, for example, for creating paginated responses. Instead of using @{…}, you add a question mark (@?{…}) to indicate the presence of a query string. The value you provide must be a two-tuple with the first value being a type-safe URL and the second being a list of query string parameter pairs. See the next code snippet for an example.

  1. {-# LANGUAGE QuasiQuotes #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. import Text.Hamlet (HtmlUrl, hamlet)
  4. import Text.Blaze.Html.Renderer.String (renderHtml)
  5. import Data.Text (Text, append, pack)
  6. import Control.Arrow (second)
  7. import Network.HTTP.Types (renderQueryText)
  8. import Data.Text.Encoding (decodeUtf8)
  9. import Blaze.ByteString.Builder (toByteString)
  10. data MyRoute = SomePage
  11. render :: MyRoute -> [(Text, Text)] -> Text
  12. render SomePage params = "/home" `append`
  13. decodeUtf8 (toByteString $ renderQueryText True (map (second Just) params))
  14. main :: IO ()
  15. main = do
  16. let currPage = 2 :: Int
  17. putStrLn $ renderHtml $ [hamlet|
  18. <p>
  19. You are currently on page #{currPage}.
  20. <a href=@?{(SomePage, [("page", pack $ show $ currPage - 1)])}>Previous
  21. <a href=@?{(SomePage, [("page", pack $ show $ currPage + 1)])}>Next
  22. |] render

This generates the expected HTML:

  1. <p>You are currently on page 2.
  2. <a href="/home?page=1">Previous</a>
  3. <a href="/home?page=3">Next</a>
  4. </p>

Attributes

In that last example, we put an href attribute on the “a” tag. Let’s elaborate on the syntax:

  • You can have interpolations within the attribute value.

  • The equals sign and value for an attribute are optional, just like in HTML. So <input type=checkbox checked> is perfectly valid.

  • There are two convenience attributes: for id, you can use the hash, and for classes, the period. In other words, <p #paragraphid .class1 .class2>.

  • While quotes around the attribute value are optional, they are required if you want to embed spaces.

  • You can add an attribute optionally by using colons. To make a checkbox only checked if the variable isChecked is True, you would write <input type=checkbox :isChecked:checked>. To have a paragraph be optionally red, you could use <p :isRed:style="color:red">. (This also works for class names, e.g., <p :isCurrent:.current> will set the class current if isCurrent is True.)

  • Arbitrary key-value pairs can also be interpolated using the *{…} syntax. The interpolated variable must be a tuple, or list of tuples, of Text or String. For example: if we have a variable attrs = [("foo", "bar")], we could interpolate that into an element like: <p *{attrs}> to get <p foo="bar">.

Conditionals

Eventually, you’ll want to put in some logic in your page. The goal of Hamlet is to make the logic as minimalistic as possible, pushing the heavy lifting into Haskell. As such, our logical statements are very basic… so basic, that it’s if, elseif, and else.

  1. $if isAdmin
  2. <p>Welcome to the admin section.
  3. $elseif isLoggedIn
  4. <p>You are not the administrator.
  5. $else
  6. <p>I don't know who you are. Please log in so I can decide if you get access.

All the same rules of normal interpolation apply to the content of the conditionals.

Maybe

Similarly, we have a special construct for dealing with Maybe values. This could technically be dealt with using if, isJust and fromJust, but this is more convenient and avoids partial functions.

  1. $maybe name <- maybeName
  2. <p>Your name is #{name}
  3. $nothing
  4. <p>I don't know your name.

In addition to simple identifiers, you can use a few other, more complicated values on the left hand side, such as constructors and tuples.

  1. $maybe Person firstName lastName <- maybePerson
  2. <p>Your name is #{firstName} #{lastName}

The right-hand-side follows the same rules as interpolations, allow variables, function application, and so on.

Forall

And what about looping over lists? We have you covered there too:

  1. $if null people
  2. <p>No people.
  3. $else
  4. <ul>
  5. $forall person <- people
  6. <li>#{person}

Case

Pattern matching is one of the great strengths of Haskell. Sum types let you cleanly model many real-world types, and case statements let you safely match, letting the compiler warn you if you missed a case. Hamlet gives you the same power.

  1. $case foo
  2. $of Left bar
  3. <p>It was left: #{bar}
  4. $of Right baz
  5. <p>It was right: #{baz}

With

Rounding out our statements, we have with. It’s basically just a convenience for declaring a synonym for a long expression.

  1. $with foo <- some very (long ugly) expression that $ should only $ happen once
  2. <p>But I'm going to use #{foo} multiple times. #{foo}

Doctype

Last bit of syntactic sugar: the doctype statement. We have support for a number of different versions of a doctype, though we recommend $doctype 5 for modern web applications, which generates <!DOCTYPE html>.

  1. $doctype 5
  2. <html>
  3. <head>
  4. <title>Hamlet is Awesome
  5. <body>
  6. <p>All done.

There is an older and still supported syntax: three exclamation points (!!!). You may still see this in code out there. We have no plans to remove support for this, but in general find the $doctype approach easier to read.

Lucius Syntax

Lucius is one of two CSS templating languages in the Shakespeare family. It is intended to be a superset of CSS, leveraging the existing syntax while adding in a few more features.

  • Like Hamlet, we allow both variable and URL interpolation.

  • CSS blocks are allowed to nest.

  • You can declare variables in your templates.

  • A set of CSS properties can be created as a mixin, and reused in multiple declarations.

Starting with the second point: let’s say you want to have some special styling for some tags within your article. In plain ol’ CSS, you’d have to write:

  1. article code { background-color: grey; }
  2. article p { text-indent: 2em; }
  3. article a { text-decoration: none; }

In this case, there aren’t that many clauses, but having to type out article each time is still a bit of a nuisance. Imagine if you had a dozen or so of these. Not the worst thing in the world, but a bit of an annoyance. Lucius helps you out here:

  1. article {
  2. code { background-color: grey; }
  3. p { text-indent: 2em; }
  4. a { text-decoration: none; }
  5. > h1 { color: green; }
  6. }

Having Lucius variables allows you to avoid repeating yourself. A simple example would be to define a commonly used color:

  1. @textcolor: #ccc; /* just because we hate our users */
  2. body { color: #{textcolor} }
  3. a:link, a:visited { color: #{textcolor} }

Mixins are a relatively new addition to Lucius. The idea is to declare a mixin providing a collection of properties, and then embed that mixin in a template using caret interpolation (^). The following example demonstrates how we could use a mixin to deal with vendor prefixes.

  1. {-# LANGUAGE QuasiQuotes #-}
  2. import Text.Lucius
  3. import qualified Data.Text.Lazy.IO as TLIO
  4. -- Dummy render function.
  5. render = undefined
  6. -- Our mixin, which provides a number of vendor prefixes for transitions.
  7. transition val =
  8. [luciusMixin|
  9. -webkit-transition: #{val};
  10. -moz-transition: #{val};
  11. -ms-transition: #{val};
  12. -o-transition: #{val};
  13. transition: #{val};
  14. |]
  15. -- Our actual Lucius template, which uses the mixin.
  16. myCSS =
  17. [lucius|
  18. .some-class {
  19. ^{transition "all 4s ease"}
  20. }
  21. |]
  22. main = TLIO.putStrLn $ renderCss $ myCSS render

Cassius Syntax

Cassius is a whitespace-sensitive alternative to Lucius. As mentioned in the synopsis, it uses the same processing engine as Lucius, but preprocesses all input to insert braces to enclose subblocks and semicolons to terminate lines. This means you can leverage all features of Lucius when writing Cassius. As a simple example:

  1. #banner
  2. border: 1px solid #{bannerColor}
  3. background-image: url(@{BannerImageR})

Julius Syntax

Julius is the simplest of the languages discussed here. In fact, some might even say it’s really just Javascript. Julius allows the three forms of interpolation we’ve mentioned so far, and otherwise applies no transformations to your content.

If you use Julius with the scaffolded Yesod site, you may notice that your Javascript is automatically minified. This is not a feature of Julius; instead, Yesod uses the hjsmin package to minify Julius output.

Calling Shakespeare

The question of course arises at some point: how do I actually use this stuff? There are three different ways to call out to Shakespeare from your Haskell code:

Quasiquotes

Quasiquotes allow you to embed arbitrary content within your Haskell, and for it to be converted into Haskell code at compile time.

External file

In this case, the template code is in a separate file which is referenced via Template Haskell.

Reload mode

Both of the above modes require a full recompile to see any changes. In reload mode, your template is kept in a separate file and referenced via Template Haskell. But at runtime, the external file is reparsed from scratch each time.

Reload mode is not available for Hamlet, only for Cassius, Lucius and Julius. There are too many sophisticated features in Hamlet that rely directly on the Haskell compiler and could not feasibly be reimplemented at runtime.

One of the first two approaches should be used in production. They both embed the entirety of the template in the final executable, simplifying deployment and increasing performance. The advantage of the quasiquoter is the simplicity: everything stays in a single file. For short templates, this can be a very good fit. However, in general, the external file approach is recommended because:

  • It follows nicely in the tradition of separating logic from presentation.

  • You can easily switch between external file and debug mode with some simple CPP macros, meaning you can keep rapid development and still achieve high performance in production.

Since these are special QuasiQuoters and Template Haskell functions, you need to be sure to enable the appropriate language extensions and use correct syntax. You can see a simple example of each in the following code snippets.

Quasiquoter

  1. {-# LANGUAGE OverloadedStrings #-} -- we're using Text below
  2. {-# LANGUAGE QuasiQuotes #-}
  3. import Text.Hamlet (HtmlUrl, hamlet)
  4. import Data.Text (Text)
  5. import Text.Blaze.Html.Renderer.String (renderHtml)
  6. data MyRoute = Home | Time | Stylesheet
  7. render :: MyRoute -> [(Text, Text)] -> Text
  8. render Home _ = "/home"
  9. render Time _ = "/time"
  10. render Stylesheet _ = "/style.css"
  11. template :: Text -> HtmlUrl MyRoute
  12. template title = [hamlet|
  13. $doctype 5
  14. <html>
  15. <head>
  16. <title>#{title}
  17. <link rel=stylesheet href=@{Stylesheet}>
  18. <body>
  19. <h1>#{title}
  20. |]
  21. main :: IO ()
  22. main = putStrLn $ renderHtml $ template "My Title" render

External file

  1. {-# LANGUAGE OverloadedStrings #-} -- we're using Text below
  2. {-# LANGUAGE TemplateHaskell #-}
  3. {-# LANGUAGE CPP #-} -- to control production versus debug
  4. import Text.Lucius (CssUrl, luciusFile, luciusFileReload, renderCss)
  5. import Data.Text (Text)
  6. import qualified Data.Text.Lazy.IO as TLIO
  7. data MyRoute = Home | Time | Stylesheet
  8. render :: MyRoute -> [(Text, Text)] -> Text
  9. render Home _ = "/home"
  10. render Time _ = "/time"
  11. render Stylesheet _ = "/style.css"
  12. template :: CssUrl MyRoute
  13. #if PRODUCTION
  14. template = $(luciusFile "template.lucius")
  15. #else
  16. template = $(luciusFileReload "template.lucius")
  17. #endif
  18. main :: IO ()
  19. main = TLIO.putStrLn $ renderCss $ template render
  1. /* @template.lucius */
  2. foo { bar: baz }

The naming scheme for the functions is very consistent.

LanguageQuasiquoterExternal fileReload

Hamlet

hamlet

hamletFile

N/A

Cassius

cassius

cassiusFile

cassiusFileReload

Lucius

lucius

luciusFile

luciusFileReload

Julius

julius

juliusFile

juliusFileReload

Alternate Hamlet Types

So far, we’ve seen how to generate an HtmlUrl value from Hamlet, which is a piece of HTML with embedded type-safe URLs. There are currently three other values we can generate using Hamlet: plain HTML, HTML with URLs and internationalized messages, and widgets. That last one will be covered in more detail in the widgets chapter.

To generate plain HTML without any embedded URLs, we use “simplified Hamlet”. There are a few changes:

  • We use a different set of functions, prefixed with an “s”. So the quasiquoter is shamlet and the external file function is shamletFile. How we pronounce those is still up for debate.

  • No URL interpolation is allowed. Doing so will result in a compile-time error.

  • Embedding (the caret-interpolator) no longer allows arbitrary HtmlUrl values. The rule is that the embedded value must have the same type as the template itself, so in this case it must be Html. That means that for shamlet, embedding can be completely replaced with normal variable interpolation (with a hash).

Dealing with internationalization (i18n) in Hamlet is a bit complicated. Hamlet supports i18n via a message datatype, very similar in concept and implementation to a type-safe URL. As a motivating example, let’s say we want to have an application that tells you hello and how many apples you bought. We could represent those messages with a datatype.

  1. data Msg = Hello | Apples Int

Next, we would want to be able to convert that into something human-readable, so we define some render functions:

  1. renderEnglish :: Msg -> Text
  2. renderEnglish Hello = "Hello"
  3. renderEnglish (Apples 0) = "You did not buy any apples."
  4. renderEnglish (Apples 1) = "You bought 1 apple."
  5. renderEnglish (Apples i) = T.concat ["You bought ", T.pack $ show i, " apples."]

Now we want to interpolate those Msg values directly in the template. For that, we use underscore interpolation.

  1. $doctype 5
  2. <html>
  3. <head>
  4. <title>i18n
  5. <body>
  6. <h1>_{Hello}
  7. <p>_{Apples count}

This kind of a template now needs some way to turn those values into HTML. So just like type-safe URLs, we pass in a render function. To represent this, we define a new type synonym:

  1. type Render url = url -> [(Text, Text)] -> Text
  2. type Translate msg = msg -> Html
  3. type HtmlUrlI18n msg url = Translate msg -> Render url -> Html

At this point, you can pass renderEnglish, renderSpanish, or renderKlingon to this template, and it will generate nicely translated output (depending, of course, on the quality of your translators). The complete program is:

  1. {-# LANGUAGE QuasiQuotes #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. import Data.Text (Text)
  4. import qualified Data.Text as T
  5. import Text.Hamlet (HtmlUrlI18n, ihamlet)
  6. import Text.Blaze.Html (toHtml)
  7. import Text.Blaze.Html.Renderer.String (renderHtml)
  8. data MyRoute = Home | Time | Stylesheet
  9. renderUrl :: MyRoute -> [(Text, Text)] -> Text
  10. renderUrl Home _ = "/home"
  11. renderUrl Time _ = "/time"
  12. renderUrl Stylesheet _ = "/style.css"
  13. data Msg = Hello | Apples Int
  14. renderEnglish :: Msg -> Text
  15. renderEnglish Hello = "Hello"
  16. renderEnglish (Apples 0) = "You did not buy any apples."
  17. renderEnglish (Apples 1) = "You bought 1 apple."
  18. renderEnglish (Apples i) = T.concat ["You bought ", T.pack $ show i, " apples."]
  19. template :: Int -> HtmlUrlI18n Msg MyRoute
  20. template count = [ihamlet|
  21. $doctype 5
  22. <html>
  23. <head>
  24. <title>i18n
  25. <body>
  26. <h1>_{Hello}
  27. <p>_{Apples count}
  28. |]
  29. main :: IO ()
  30. main = putStrLn $ renderHtml
  31. $ (template 5) (toHtml . renderEnglish) renderUrl

Other Shakespeare

In addition to HTML, CSS and Javascript helpers, there is also some more general-purpose Shakespeare available. shakespeare-text provides a simple way to create interpolated strings, much like people are accustomed to in scripting languages like Ruby and Python. This package’s utility is definitely not limited to Yesod.

  1. {-# LANGUAGE QuasiQuotes, OverloadedStrings #-}
  2. import Text.Shakespeare.Text
  3. import qualified Data.Text.Lazy.IO as TLIO
  4. import Data.Text (Text)
  5. import Control.Monad (forM_)
  6. data Item = Item
  7. { itemName :: Text
  8. , itemQty :: Int
  9. }
  10. items :: [Item]
  11. items =
  12. [ Item "apples" 5
  13. , Item "bananas" 10
  14. ]
  15. main :: IO ()
  16. main = forM_ items $ \item -> TLIO.putStrLn
  17. [lt|You have #{show $ itemQty item} #{itemName item}.|]

Some quick points about this simple example:

  • Notice that we have three different textual datatypes involved (String, strict Text and lazy Text). They all play together well.

  • We use a quasiquoter named lt, which generates lazy text. There is also st.

  • Also, there are longer names for these quasiquoters (ltext and stext).

  • The syntax for variable interpolation for Text.Shakespeare.Text is the same as described above. Note that ^{..} and @{..} are also recognized in lt and st. If the output of a template should contain ^{..}, a backslash can be used to prevent the interpolation, e.g. [lt|2^\{\23}|]. The backslash is removed from the resulting text.

General Recommendations

Here are some general hints from the Yesod community on how to get the most out of Shakespeare.

  • For actual sites, use external files. For libraries, it’s OK to use quasiquoters, assuming they aren’t too long.

  • Patrick Brisbin has put together a Vim code highlighter that can help out immensely.

  • You should almost always start Hamlet tags on their own line instead of embedding start/end tags after an existing tag. The only exception to this is the occasional <i> or <b> tag inside a large block of text.