@@ -44,10 +44,12 @@
4444module Hakyll.Web.Template
4545 ( Template
4646 , applyTemplate
47+ , applyTemplateWith
4748 , applySelf
4849 , templateCompiler
4950 , templateCompilerWith
5051 , applyTemplateCompiler
52+ , applyTemplateCompilerWith
5153 ) where
5254
5355import Control.Arrow
@@ -65,17 +67,29 @@ import Hakyll.Web.Template.Read
6567import Hakyll.Web.Page.Internal
6668
6769-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
68−-- "Page". When a key is not found, it is left as it is. You can specify
69−-- the characters used to replace escaped dollars (@$$@) here.
70+-- "Page". When a key is not found, it is left as it is.
7071--
7172applyTemplate :: Template -> Page String -> Page String
72−applyTemplate template page =
73+applyTemplate = applyTemplateWith defaultMissingHandler
74+
75+-- | Default solution if a key is missing: render it again
76+defaultMissingHandler :: String -> String
77+defaultMissingHandler k = "$" ++ k ++ "$"
78+
79+-- | A version of 'applyTemplate' which allows you to give a fallback option,
80+-- which can produce the value for a key if it is missing.
81+--
82+applyTemplateWith :: (String -> String) -- ^ Fallback if key missing
83+ -> Template -- ^ Template to apply
84+ -> Page String -- ^ Input page
85+ -> Page String -- ^ Resulting page
86+applyTemplateWith missing template page =
7387 fmap (const $ substitute =<< unTemplate template) page
7488 where
7589 map' = toMap page
7690 substitute (Chunk chunk) = chunk
77− substitute (Key key) = fromMaybe ("$" ++ key ++ "$") $ M.lookup key map'
78− substitute (Escaped) = "$"
91+ substitute (Key key) = fromMaybe (missing key) $ M.lookup key map'
92+ substitute (Escaped) = "$"
7993
8094-- | Apply a page as it's own template. This is often very useful to fill in
8195-- certain keys like @$root@ and @$url@.
@@ -106,4 +120,13 @@ templateCompilerWith settings =
106120
107121applyTemplateCompiler :: Identifier Template -- ^ Template
108122 -> Compiler (Page String) (Page String) -- ^ Compiler
109−applyTemplateCompiler identifier = require identifier (flip applyTemplate)
123+applyTemplateCompiler = applyTemplateCompilerWith defaultMissingHandler
124+
125+-- | A version of 'applyTemplateCompiler' which allows you to pass a function
126+-- which is called for a key when it is missing.
127+--
128+applyTemplateCompilerWith :: (String -> String)
129+ -> Identifier Template
130+ -> Compiler (Page String) (Page String)
131+applyTemplateCompilerWith missing identifier =
132+ require identifier (flip $ applyTemplateWith missing)