From 31160aa7e640c5d52a47221b630168b55386af01 Mon Sep 17 00:00:00 2001 From: 0xd34df00d <0xd34df00d@gmail.com> Date: Wed, 18 Sep 2024 14:01:42 -0500 Subject: [PATCH] Add boolFieldM for monadic bool fields creation --- lib/Hakyll/Web/Template/Context.hs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/Hakyll/Web/Template/Context.hs b/lib/Hakyll/Web/Template/Context.hs index 8a60fdad..8bd5c6b3 100644 --- a/lib/Hakyll/Web/Template/Context.hs +++ b/lib/Hakyll/Web/Template/Context.hs @@ -26,6 +26,7 @@ module Hakyll.Web.Template.Context , Context (..) , field , boolField + , boolFieldM , constField , listField , listFieldWith @@ -147,7 +148,17 @@ boolField :: String -> (Item a -> Bool) -> Context a -boolField name f = field' name (\i -> if f i +boolField name f = boolFieldM name (pure . f) + +-- | Creates a 'field' to use with the @$if()$@ template macro, in the 'Compiler' monad. +-- Attempting to substitute the field into the template will cause an error. +boolFieldM + :: String + -> (Item a -> Compiler Bool) + -> Context a +boolFieldM name f = field' name (\i -> do + b <- f i + if b then return EmptyField else noResult $ "Field " ++ name ++ " is false")