-- | This module provides means for reading and applying 'Template's. -- -- Templates are tools to convert data (pages) into a string. They are -- perfectly suited for laying out your site. -- -- Let's look at an example template: -- -- > -- > -- > My crazy homepage - $title$ -- > -- > -- > -- >
-- > $body$ -- >
-- > -- > -- > -- -- We can use this template to render a 'Page' which has a body and a @$title$@ -- metadata field. -- -- As you can see, the format is very simple -- @$key$@ is used to render the -- @$key$@ field from the page, everything else is literally copied. If you want -- to literally insert @\"$key$\"@ into your page (for example, when you're -- writing a Hakyll tutorial) you can use -- -- >

-- > A literal $$key$$. -- >

-- -- Because of it's simplicity, these templates can be used for more than HTML: -- you could make, for example, CSS or JS templates as well. module Hakyll.Web.Template ( Template , templateCompiler , applyTemplate , applyTemplateWith ) where -------------------------------------------------------------------------------- import Control.Monad (forM, liftM) import Prelude hiding (id) -------------------------------------------------------------------------------- import Hakyll.Core.Compiler import Hakyll.Core.Item import Hakyll.Web.Template.Context import Hakyll.Web.Template.Internal import Hakyll.Web.Template.Read -------------------------------------------------------------------------------- -- | Read a template. templateCompiler :: Compiler (Item Template) templateCompiler = cached "Hakyll.Web.Template.templateCompiler" $ do item <- getResourceString return $ fmap readTemplate item -------------------------------------------------------------------------------- applyTemplate :: Template -- ^ Template -> Context a -- ^ Context -> Item a -- ^ Page -> Compiler (Item String) -- ^ Resulting item applyTemplate tpl context item = do let context' k x = unContext context k x body <- applyTemplateWith context' tpl item return $ itemSetBody body item -------------------------------------------------------------------------------- applyTemplateWith :: Monad m => (String -> a -> m String) -> Template -> a -> m String applyTemplateWith context tpl x = liftM concat $ forM (unTemplate tpl) $ \e -> case e of Chunk c -> return c Escaped -> return "$" Key k -> context k x