diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-11-21 09:26:54 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-11-21 09:26:54 +0100 |
commit | 6b4c65642e21684bc143eaf29453d1d99fd9e227 (patch) | |
tree | 1eac7fe2bd1d25a198737fee759363f7bbdd7f3a /src | |
parent | 815c265ac6135e524a4bec06f90e358b44acedd2 (diff) | |
download | hakyll-6b4c65642e21684bc143eaf29453d1d99fd9e227.tar.gz |
Add requireApplyTemplate and applySelf utilities
Diffstat (limited to 'src')
-rw-r--r-- | src/Hakyll/Web/Template.hs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/Hakyll/Web/Template.hs b/src/Hakyll/Web/Template.hs index a933496..3eaae6b 100644 --- a/src/Hakyll/Web/Template.hs +++ b/src/Hakyll/Web/Template.hs @@ -40,6 +40,8 @@ module Hakyll.Web.Template ( Template , templateCompiler , applyTemplate + , requireApplyTemplate + , applySelf , applyTemplateWith ) where @@ -51,6 +53,7 @@ import Prelude hiding (id) -------------------------------------------------------------------------------- import Hakyll.Core.Compiler +import Hakyll.Core.Identifier import Hakyll.Core.Item import Hakyll.Web.Template.Context import Hakyll.Web.Template.Internal @@ -77,6 +80,39 @@ applyTemplate tpl context item = do -------------------------------------------------------------------------------- +-- | The following pattern is so common: +-- +-- > tpl <- requireBody "templates/foo.html" +-- > someCompiler +-- > >>= applyTemplate tpl context +-- +-- That we have a single function which does this: +-- +-- > someCompiler +-- > >>= requireApplyTemplate "templates/foo.html" context +requireApplyTemplate :: Identifier -- ^ Template identifier + -> Context a -- ^ Context + -> Item a -- ^ Page + -> Compiler (Item String) -- ^ Resulting item +requireApplyTemplate identifier context item = do + tpl <- requireBody identifier + applyTemplate tpl context item + + +-------------------------------------------------------------------------------- +-- | It is also possible that you want to substitute @$key$@s within the body of +-- an item. This function does that by interpreting the item body as a template, +-- and then applying it to itself. +applySelf :: Context String -- ^ Context + -> Item String -- ^ Item and template + -> Compiler (Item String) -- ^ Resulting item +applySelf context item = + let tpl = readTemplate $ itemBody item + in applyTemplate tpl context item + + +-------------------------------------------------------------------------------- +-- | Overloaded apply template function to work in an arbitrary Monad. applyTemplateWith :: Monad m => (String -> a -> m String) -> Template -> a -> m String |