@@ -44,10 +44,12 @@
44module Hakyll.Web.Template
44module Hakyll.Web.Template
45 ( Template
45 ( Template
46 , applyTemplate
46 , applyTemplate
47 , applySelf
48 , applySelf
48 , templateCompiler
49 , templateCompiler
49 , templateCompilerWith
50 , templateCompilerWith
50 , applyTemplateCompiler
51 , applyTemplateCompiler
52+ , applyTemplateCompilerWith
53import Control.Arrow
55import Control.Arrow
@@ -65,17 +67,29 @@ import Hakyll.Web.Template.Read
65import Hakyll.Web.Page.Internal
67import Hakyll.Web.Page.Internal
67-- | Substitutes @$identifiers@ in the given @Template@ by values from the given
69-- | 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
70+-- "Page". When a key is not found, it is left as it is.
69−-- the characters used to replace escaped dollars (@$$@) here.
71applyTemplate :: Template -> Page String -> Page String
72applyTemplate :: Template -> Page String -> Page String
72−applyTemplate template page =
73+applyTemplate = applyTemplateWith defaultMissingHandler
75+-- | Default solution if a key is missing: render it again
76+defaultMissingHandler :: String -> String
77+defaultMissingHandler k = "$" ++ k ++ "$"
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.
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 =
73 fmap (const $ substitute =<< unTemplate template) page
87 fmap (const $ substitute =<< unTemplate template) page
75 map' = toMap page
89 map' = toMap page
76 substitute (Chunk chunk) = chunk
90 substitute (Chunk chunk) = chunk
77− substitute (Key key) = fromMaybe ("$" ++ key ++ "$") $ M.lookup key map'
91+ substitute (Key key) = fromMaybe (missing key) $ M.lookup key map'
78− substitute (Escaped) = "$"
92+ substitute (Escaped) = "$"
80-- | Apply a page as it's own template. This is often very useful to fill in
94-- | Apply a page as it's own template. This is often very useful to fill in
81-- certain keys like @$root@ and @$url@.
95-- certain keys like @$root@ and @$url@.
@@ -106,4 +120,13 @@ templateCompilerWith settings =
107applyTemplateCompiler :: Identifier Template -- ^ Template
121applyTemplateCompiler :: Identifier Template -- ^ Template
108 -> Compiler (Page String) (Page String) -- ^ Compiler
122 -> Compiler (Page String) (Page String) -- ^ Compiler
109−applyTemplateCompiler identifier = require identifier (flip applyTemplate)
123+applyTemplateCompiler = applyTemplateCompilerWith defaultMissingHandler
125+-- | A version of 'applyTemplateCompiler' which allows you to pass a function
126+-- which is called for a key when it is missing.
128+applyTemplateCompilerWith :: (String -> String)
129+ -> Identifier Template
130+ -> Compiler (Page String) (Page String)
131+applyTemplateCompilerWith missing identifier =
132+ require identifier (flip $ applyTemplateWith missing)