From 83900909a12c6b6a3c8f7ec1433034fb83994312 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Wed, 12 Nov 2014 19:41:20 +0200 Subject: [PATCH] Add maybeField This field allows to easily introduce optional keys which can then be conditionally rendered in templates. --- src/Hakyll/Web/Template/Context.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs index 2da76d4bc..5fba051bf 100644 --- a/src/Hakyll/Web/Template/Context.hs +++ b/src/Hakyll/Web/Template/Context.hs @@ -5,6 +5,7 @@ module Hakyll.Web.Template.Context , Context (..) , field , constField + , maybeField , listField , listFieldWith , functionField @@ -28,7 +29,7 @@ module Hakyll.Web.Template.Context -------------------------------------------------------------------------------- import Control.Applicative (Alternative (..), (<$>)) -import Control.Monad (msum) +import Control.Monad (msum, (<=<)) import Data.List (intercalate) import qualified Data.Map as M import Data.Monoid (Monoid (..)) @@ -102,6 +103,11 @@ field key value = field' key (fmap StringField . value) constField :: String -> String -> Context a constField key = field key . const . return +-------------------------------------------------------------------------------- +-- | 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 = field key $ maybe empty return <=< value -------------------------------------------------------------------------------- listField :: String -> Context a -> Compiler [Item a] -> Context b