3

I'm looking for simple and easy way to add template or layouts functionality to a web site in servant. So when I have a few similar pages, I don't have to create a layout for each of them completely from scratch, I can create a master or layout page and then each page can inherit it and change the html layout appropriately for itself. Everyone is familiar with that.

How can I do that? Currently I have using below code,

type API = 
  "items" :> Get '[JSON] [MyData] :<|>
  Raw

app :: Application
app = serve api server

api :: Proxy API
api = Proxy

server :: Server API
server = 
  getItems :<|> 
  serveDirectory "my_html"

startApp :: IO ()
startApp = run 3333 app

Note that I'd not like to use an approach where you have to desribe the html layout in Haskell code.

Yolanda
  • 41
  • 4
  • Could you clarify what you mean by the last sentence? – leftaroundabout Apr 09 '16 at 14:52
  • @leftaroundabout, I want to read a template from an html file, not to create it staticaly in Haskell. – Yolanda Apr 09 '16 at 14:56
  • Ah. Well, I reckon it would become a heck lot _simpler_ if you defined it in Haskell (then you could just make the master layout _parametric_ on anything you wish to customise), but it should also be possible with an externally-read html file. – leftaroundabout Apr 09 '16 at 14:59
  • @leftaroundabout, in haskell an html layout? what if it's complex? and re-compile each time I change it? – Yolanda Apr 09 '16 at 15:00
  • @leftaroundabout, " but it should also be possible with an externally-read html file." -- how to do that? – Yolanda Apr 09 '16 at 15:01
  • Well, IMO, the more complex something is, the more reason to do it in Haskell! Recompiling after a change is also quite a good idea, because it immediately catches most errors you might make. — That said, I acknowledge you may have good reasons to stick with plain html; as I said it _should_ be possible, but you'll need to parse the html and put it back. There are probably some nice ways to do this with lenses, but I'm not an expert. – leftaroundabout Apr 09 '16 at 15:18
  • You are probably looking for something like [shakespeare](http://hackage.haskell.org/package/shakespeare) which allows you to write HTML in actual HTML syntax inside a Haskell source file (or parse it from a file). It's designed for yesod but there is no actual dependency on yesod so you can plug it into any web framework. It looks like [there is](http://hackage.haskell.org/package/servant-ede-0.5.1) something similar for servant but it isn't maintained nor have I ever used it. – user2407038 Apr 09 '16 at 17:18
  • Have you seen [servant-ede](http://hackage.haskell.org/package/servant-ede)? It shows one way you can do templating with servant. I'm sure other approaches exist. – Alp Mestanogullari Apr 09 '16 at 18:17

0 Answers0