Layout Format
Layouts are full HTML documents in src/layouts/ that wrap a page. This page covers the three things Cook adds on top of plain layouts: layouts that are built on other layouts, data-if and data-unless sections, and a layout format (format 1) with a checker, cook check-layouts.
All of it is opt-in. A site that uses none of it builds byte-for-byte as before.
Layouts built on layouts
A layout whose <html> or <body> has data-template="parent" is a child layout. Cook applies the child to the page, then applies the result to the parent. The child's body becomes the parent's content, each <template slot="x"> fills the parent's slot x, and the other data-* attributes on the body become variables the parent can use.
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>${title} · Cook SSG</title> ${slot:head}</head><body data-page-layout="${pageLayout}"> <main>${content}</main> <aside data-if="slot:aside">${slot:aside}</aside></body></html>
<body data-template="base" data-page-layout="article"> <article> <h1>${title}</h1> <p data-if="date"><time datetime="${date}">${date}</time></p> ${content} </article> <template slot="aside">${slot:aside}</template></body>
Here data-page-layout="article" on the child reaches the base as the pageLayout variable.
- A chain can be five layouts deep. Longer, or a cycle, is a build error that names the chain.
- A child fills only the parent's slots it names. To let a page fill a slot of the parent, the child forwards it, as
post.htmldoes foraside. A forwarded slot the page left empty counts as empty, so the parent'sdata-if="slot:aside"removes the element. - It works for HTML pages and for Markdown pages.
- Page content is never read as layout structure. A page that contains
<main>,<template>or a template string reaches the output exactly as written.
A page picks a layout as before: layout: in front matter, or data-template on <html> or <body>.
Conditional sections
data-if="key" keeps an element only when the key is set. data-unless="key" keeps it only when the key is not set. Cook then removes the attribute.
<img data-if="image" src="${image}" alt="${imageAlt}"><p data-unless="image">No picture for this one.</p><aside data-if="slot:aside">${slot:aside}</aside>
A key is not set when it is missing, an empty string (or only spaces), an empty list or false. A key can be:
- a page field (
image), or a site field fromconfig/data.js; slot:name, for a slot the page filled;content, set when the page produced any content (testing it doesn't count as the layout's onecontentplaceholder);item.field, inside adata-repeat.
Inside a repeat
Inside a data-repeat, a key must use the item's alias. A bare key refers to the page, never to the item.
<article data-repeat="collections.blog | limit:5 as post"> <img data-if="post.image" src="${post.image}" alt=""> <h2>${post.title}</h2></article>
Writing data-if="image" here would test the page's own image, not each post's.
Where they are evaluated
Cook evaluates conditionals at layout time (page fields, slots and site data), for each item of a data-repeat, and in a last pass over the finished page. That last pass looks at the whole page, content included. An example of data-if or data-unless written as real markup in your page content is evaluated like any other. To show one, put it in escaped code, as the examples on this page do (<code-block> with <).
On Markdown pages, the page's fields are decided at layout time. By the time includes are processed the front matter is gone, so a conditional inside an include cannot test a page field there and sees it as empty. Test page fields in the layout itself.
The layout format (format 1)
Format 1 is plain Cook HTML with a small, fixed vocabulary, plus a checker that says whether a file follows it. Hand-written layouts can follow it, and tools such as the Vanilla Press layout editor only open files that do.
A layout is plain until it carries an info block. Plain layouts build exactly as before and are never failures.
The info block
<script type="application/json" data-layout-info>{ "format": 1, "label": "Blog post", "description": "A dated article.", "abstract": false, "slots": { "aside": { "label": "Sidebar" } } }</script>
- In a base layout it goes in
<head>. In a child it is the first thing in<body>. - Cook removes it from the output.
abstract: truemeans the layout exists only to be extended; editors do not offer it to pages.slotslists every slot the layout uses, and nothing else.headis a reserved slot. A base may put it last in<head>, and a child fills it with<template slot="head">holding onlymeta,link,scriptandstyle.
The blocks
Inside main, header, nav, aside, footer, a child's body, slot templates and fragments, every element must be one of these.
| Block | Markup |
|---|---|
| Content | the content placeholder, exactly once, inside main (base) or the child body |
| Slot | a slot:name placeholder, alone inside its parent element |
| Text | h1 to h6, p, small, figcaption, blockquote, holding text, fields and a, strong, em, span, time, br, img, abbr, code, small, mark |
| Heading group | hgroup holding exactly one heading (h1 to h6) and any p (it is a block, never inside a p or a heading) |
| Image | img, picture with source, or a figure around them, with an optional figcaption |
| Container | a Vanilla Breeze layout-* element, or div, section, article, header, footer, nav, aside carrying data-layout (sectioning elements need no attribute) |
| List | any block with data-repeat="source | limit:N as item", such as a container, a ul or an li in a menu (see below); Cook clones the element it sits on, and its fields are item.x |
| Menu | ul, ol or menu of li items |
| Include | an empty <div data-include="/includes/x.html"></div> |
| Component | any custom element (hyphenated tag), kept as written; it may carry data-repeat, and its attributes can use the item's fields |
| Comment | an HTML comment |
Any block may carry id, class, role, lang, aria-*, data-*, data-if and data-unless.
A base layout's <body> has data-page-layout set to one of stack, sidebar-left, sidebar-right, holy-grail, app-shell, dashboard, article, landing (or empty), or to the pageLayout variable so a child can choose. Its children are header, nav (at most two), exactly one main, aside, footer and includes. <head> holds only meta, link, title, script, style and base.
<nav><ul> <li data-repeat="navItems | limit:12 as item"><a href="${item.href}">${item.label}</a></li></ul></nav>
List sources are collections.<name>, programs, programAreas.<area> and top-level arrays in config/data.js.
Known fields
An unknown field is an error, so typos are caught. The known fields are:
- document fields:
title,description,date,published,updated,author,tags,image,imageAlt,theme,themeScope,layout,slug,vp_id,url,pageLayout; - site fields: the top-level keys of
config/data.js; - item fields, inside a list;
- extra document fields your site lists in
config/main.js:
export default { layouts: { fields: ['ogImage'] },};
Keeping layouts out of dist
Layouts, includes and components are used during the build, not served. List them in buildOnlyPaths so they stay out of dist/:
export default { buildOnlyPaths: ['components', 'includes', 'layouts'],};
Includes as fragments
An include is checked as a format-1 fragment only when its first line is <!-- layout-fragment -->. Without that line it is a free-form include and is never checked, so existing includes keep working.
cook check-layouts
cook check-layouts
The command checks every file in src/layouts and src/includes and prints file:line: problem for each problem. A layout is only "editable" when it and every parent above it follow format 1.
- It exits 1 when any layout with an info block, or any marked fragment, has a problem (for a layout with an info block, a missing parent or a cycle in its chain counts), or when
config/data.jsorconfig/main.jsthrows while loading. - Plain layouts are listed as plain and never fail the check, even when their parent is missing or their chain has a cycle.
- It ends with a summary line:
N layouts, M includes: X editable, Y plain, Z with problems. - A site with no
config/data.jsis fine. Aconfig/data.jsthat throws fails the check, because the field names come from it.
The same checker is available to your own tools as a module:
import { checkLayout, listLayouts, FORMAT } from 'cook-ssg/layouts';const { layouts, includes, problems } = await listLayouts(process.cwd());
For tool authors
cook-ssg/layouts also exports what a layout editor needs. Everything here reads and returns plain data and never changes the build.
The layout tree
parseLayout(source, { fields, lists }) turns a layout file that follows format 1 into a tree, and serializeLayout(tree) writes the tree back as a layout file. Parsing the result again gives the same tree. The tree's shape is described at the top of scripts/utils/layouts/tree.js.
- A layout that doesn't follow the format is not parsed;
parseLayoutreports the problems instead, the same onescook check-layoutsprints. - A component's inner HTML, an inline script or style, and a comment between blocks survive a round trip. A correct layout holding something the tree has no place for is refused with a problem, never opened with that part lost.
- The tree holds text decoded:
♥in the file is♥in the tree. Written back, characters are written as themselves (♥), except&,<and>, and the invisible ones, which are written as references so they can be seen in the file: ,­,​(zero-width space) and⁠(word joiner).
import { parseLayout, serializeLayout } from 'cook-ssg/layouts';const parsed = parseLayout(source, { fields, lists });// change the tree, then:const next = serializeLayout(parsed.tree);
Where a layout is used
layoutUsage(siteDir) says which pages use which layouts, so a tool can show "Used by 3 pages" before an edit.
import { layoutUsage } from 'cook-ssg/layouts';const { pages, layouts } = await layoutUsage(process.cwd());// pages.post -> ['src/about.html', 'src/blog/hello.md']// layouts.base -> ['page', 'post']
pages[name]lists the paths, relative to the site, of pages insrc/that use the layoutname: through a front matterlayout:(HTML or markdown), through themarkdown.layoutdefault inconfig/main.jsfor a markdown page with none, or throughdata-templateon<html>or<body>.layouts[name]lists the layouts whosedata-templatenamesname, that is, the layouts built on it.- Files in
src/layouts,src/includes,src/componentsand program folders are not pages and are skipped. - It never throws. A file it can't read, or a
config/main.jsthat fails to load, is skipped.