hakyll icon indicating copy to clipboard operation
hakyll copied to clipboard

How to pass a list of something to template context?

Open corpix opened this issue 8 years ago • 1 comments

Hello. I am a haskell beginner. I am trying to create a simple website with hakyll where menus could be declared in JSON files. As I see here https://github.com/jaspervdj/hakyll/blob/67ecff7ad383640bc73d64edc2506c7cc648a134/lib/Hakyll/Web/Template/Context.hs#L119 the listField function is working with Compiler [Item a], but what I have in my program is:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UnicodeSyntax     #-}
import           Data.Aeson
import           Data.ByteString.Lazy as B
-- ...

data Menu =
  Menu { title ∷ String
       , image ∷ String
       , url   ∷ String
       } deriving (Show, Generic)

instance FromJSON Menu
instance ToJSON   Menu

getMenu ∷ String → IO (Maybe [Menu])
getMenu name = decode <$> B.readFile (name :: FilePath)

At this point I am lost.

  1. How to pass [Menu] into the template context correctly?
  2. Why do I need this Compiler ... type to just pass a list data structure into the template context?

Thank you.

corpix avatar Dec 10 '17 14:12 corpix

I'm trying to do something similar where I reference a file in the post metadata, then parse it and add it to the Context.

Here's where I've got so far: https://gist.github.com/tcyrus/59bc597395536d1095ccaa6794209cb2

tcyrus avatar Feb 16 '20 20:02 tcyrus