Working with routes

Ema gives you the freedom to use any Haskell type for representing your site routes. You don’t need complicated rewrite rules. Routes are best represented using what are known as sum types (or ADT, short for Abstract Data Type). Here’s an example of a route type:

data Route 
  = Index
  | About

This type represents two routes pointing to – the index page (/) and the about page (/about). Designing the route type is only half the job; you will also need to tell Ema how to convert it to / from the browser URL. We will explain this in the next section.

Advanced example

Here’s one possible way to design the route type for a weblog site,

data Route
  = Home 
  | Blog BlogRoute
  | Tag TagRoute

data BlogRoute
  = BlogIndex
  | BlogPost Ema.Slug

data TagRoute
  = TagListing
  | Tag Tag

newtype Tag = Tag Text

Defining hierarchical routes like this is useful if you want to render parts of your HTML as being common to only a subset of your site, such as adding a blog header to all blog pages, but not to tag pages.

Next, with our model and routes in place, we will make them work with Ema by defining their static site behaviour.

Links to this page
  • Tutorial

    Okay, but that’s just one page. But we want to add a second page. And might as well add more content than “Hello, Ema”. Let’s do that next. The first step is define the route type that corresponds to our site’s pages. Add the following:

  • Rendering HTML

    Once you have model and routes in place and constrained, the last piece of the puzzle is to write a function that takes both as arguments and returns file content (lazy bytestring, to be exact) to generate. This function can be as simple as the following:

  • Guide
    Working with routesUnless you site has a single page (index.html), you will need to manage a set of routes
  • Defining your model

    Next, we will talk about routes.

  • Defining Ema instance

    Once you have model and route types in place, we must tell the Haskell compiler that they are suitable for generating static sites. We do this by creating an instance of the Ema typeclass.

    Using some MyModel and the route Route shown in the previous section, we can create an instance as follows: