Includes

Includes let you share HTML fragments across pages. Store reusable markup in partial files and pull them in at build time -- headers, footers, <head> metadata, and anything else you repeat.

Creating an include

Create an HTML file anywhere in src/. A common convention is src/includes/:

Using includes

data-include attribute

Add data-include to any element. The element is replaced with the file content:

include-file element

Use the <include-file> custom element for a more explicit syntax:

Both syntaxes produce the same result. The element is fully replaced with the contents of the referenced file.

Self-closing meta includes

For <head> content, <meta data-include> is especially useful since it is valid HTML in <head>:

File paths

Include paths are relative to src/. You can omit the .html extension -- Cook adds it automatically if missing:

Nested includes

Included files can themselves contain includes. Cook resolves them iteratively up to 5 levels deep, which prevents infinite recursion while supporting practical nesting:

Caching

Cook caches include file content in memory during the build. If the same include is used across 50 pages, the file is read from disk once and served from the cache for subsequent uses. This makes includes fast even when shared across a large site.

Template strings in includes

Include files can contain ${variableName} template strings. Since includes are resolved before the final template string pass, your variables will be replaced after the include content is inlined into the page:

Build-only directories

Include directories like src/includes/ are typically listed in buildOnlyPaths so they are used during the build but not copied to the output:

Pipeline order

Includes are resolved early in the build pipeline -- after layout templates are applied but before data-repeat, components, and template strings. This means:

  • Layout templates can contain includes
  • Includes can contain components and template strings
  • Includes can contain other includes (up to 5 levels)
  • Includes cannot use data-repeat (repeat runs after includes)