Layouts
Hakyllbars supports several helpers to enable layouts within your pages.
@applyLayout
Layouts may be applied using the @applyLayout
helper. The item to which the template is applied is make available to
the template as the item
variable.
<!-- _layouts/main.html -->
<html>
<head>
<title>{{title}}</title>
</head>
<body>
{{item}}</body>
</html>
<!-- page.md -->
---
title: This is my page
___
{{@applyLayout 'main.html'}}
Your content here.
Layouts applied by metadata
Layouts may also be driven by metadata by using the following trick:
<!-- _layouts/main.html -->
<html>
<head>
<title>{{title}}</title>
</head>
<body>
{{item}}</body>
</html>
<!-- _layouts/from-context.html -->
{{#if layout-}}
{{@applyLayout layout-}}
{{body-}}
{{#else-}}
{{body-}}
{{#end-}}
<!-- page.md -->
---
title: This is my page
layout: main
___
Your content here. *
And then when compiling your pages, use HB.applyTemplate "_layouts/from-context.html"
:
-- Main.hs
do
compile >>= HB.applyTemplates do
getResourceBody
HB.applyContext context
HB.applyContent"_layouts/from-context.html" HB.applyTemplate
Compiling layouts
Layouts should live in the _layouts/
folder within your site source. Layouts must also be compiled before they may be
used, and this is accomplished with the following code:
templates :: Rules Dependency
= do
templates "_layouts/**" do
match $
compile
getResourceBody>>= HB.compileTemplateItem
>>= makeItem
makePatternDependency templatePattern
main :: IO ()
= do
main <- templates
templatesDependency do
rulesExtraDependencies [templatesDependency] "*.md" do
match $ setExtension "html"
route do
compile >>= HB.applyTemplates do
getResourceBody
HB.applyContext context
HB.applyContent"_layouts/from-context.html" HB.applyTemplate