@@ -40,6 +40,8 @@ module Hakyll.Web.Template
40 ( Template
40 ( Template
41 , templateCompiler
41 , templateCompiler
42 , applyTemplate
42 , applyTemplate
43+ , requireApplyTemplate
43 , applyTemplateWith
45 , applyTemplateWith
@@ -51,6 +53,7 @@ import Prelude hiding (id)
52--------------------------------------------------------------------------------
54--------------------------------------------------------------------------------
53import Hakyll.Core.Compiler
55import Hakyll.Core.Compiler
56+import Hakyll.Core.Identifier
54import Hakyll.Core.Item
57import Hakyll.Core.Item
55import Hakyll.Web.Template.Context
58import Hakyll.Web.Template.Context
56import Hakyll.Web.Template.Internal
59import Hakyll.Web.Template.Internal
@@ -77,6 +80,39 @@ applyTemplate tpl context item = do
79--------------------------------------------------------------------------------
82--------------------------------------------------------------------------------
83+-- | The following pattern is so common:
85+-- > tpl <- requireBody "templates/foo.html"
87+-- > >>= applyTemplate tpl context
89+-- That we have a single function which does this:
92+-- > >>= requireApplyTemplate "templates/foo.html" context
93+requireApplyTemplate :: Identifier -- ^ Template identifier
94+ -> Context a -- ^ Context
96+ -> Compiler (Item String) -- ^ Resulting item
97+requireApplyTemplate identifier context item = do
98+ tpl <- requireBody identifier
99+ applyTemplate tpl context item
102+--------------------------------------------------------------------------------
103+-- | It is also possible that you want to substitute @$key$@s within the body of
104+-- an item. This function does that by interpreting the item body as a template,
105+-- and then applying it to itself.
106+applySelf :: Context String -- ^ Context
107+ -> Item String -- ^ Item and template
108+ -> Compiler (Item String) -- ^ Resulting item
109+applySelf context item =
110+ let tpl = readTemplate $ itemBody item
111+ in applyTemplate tpl context item
114+--------------------------------------------------------------------------------
115+-- | Overloaded apply template function to work in an arbitrary Monad.
80applyTemplateWith :: Monad m
116applyTemplateWith :: Monad m
81 => (String -> a -> m String)
117 => (String -> a -> m String)
82 -> Template -> a -> m String
118 -> Template -> a -> m String