Chapter 17. Creating a Subsite
How many sites provide authentication systems? Or need to provide Create, Read, Update, Delete (CRUD) management of some objects? Or a blog? Or a wiki?
The theme here is that many websites include common components that can be reused throughout multiple sites. However, it is often quite difficult to get code to be modular enough to be truly plug-and-play: a component will require hooks into the routing system, usually for multiple routes, and will need some way of sharing styling information with the master site.
In Yesod, the solution is subsites. A subsite is a collection of routes and their handlers that can be easily inserted into a master site. By using type classes, it is easy to ensure that the master site provides certain capabilities, and to access the default site layout. And with type-safe URLs, it’s easy to link from the master site to subsites.
Hello World
Writing subsites is a little bit tricky, involving a number of different types. Let’s start off with a simple Hello World subsite:
{-# LANGUAGE QuasiQuotes, TypeFamilies, MultiParamTypeClasses #-}{-# LANGUAGE TemplateHaskell, FlexibleInstances, OverloadedStrings #-}importYesod-- Subsites have foundations just like master sites.dataHelloSub=HelloSub-- We have a familiar analogue from mkYesod, with just one extra parameter.-- We'll discuss that later.mkYesodSub"HelloSub"[][parseRoutes|/SubRootRGET|]-- And we'll spell out the handler type signature.getSubRootR::Yesod
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access