Skip to content

Commit

Permalink
Add maybeField
Browse files Browse the repository at this point in the history
This field allows to easily introduce optional keys which can then be
conditionally rendered in templates.
  • Loading branch information
nagisa committed Nov 12, 2014
1 parent 2e23f37 commit 6634d5f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Hakyll/Web/Template/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Hakyll.Web.Template.Context
, Context (..)
, field
, constField
, maybeField
, listField
, listFieldWith
, functionField
Expand Down Expand Up @@ -103,6 +104,21 @@ constField :: String -> String -> Context a
constField key = field key . const . return


maybeField' :: String
-> (Item a -> Compiler (Maybe ContextField))
-> Context a
maybeField' key value = Context $ \k _ i -> if k == key
then value i >>= maybe empty return
else empty

--------------------------------------------------------------------------------
-- | A 'field' which will only contain value for 'Just's. This
-- is mostly useful along with conditional template construct @$if(key)$@.
maybeField :: String
-> (Item a -> Compiler (Maybe String))
-> Context a
maybeField key value = maybeField' key (fmap (fmap StringField) . value)

--------------------------------------------------------------------------------
listField :: String -> Context a -> Compiler [Item a] -> Context b
listField key c xs = listFieldWith key c (const xs)
Expand Down

0 comments on commit 6634d5f

Please sign in to comment.