No description
Find a file
2016-04-16 08:36:45 -06:00
lib Replace backslashes in partial names with expected forward slashes 2016-04-16 07:58:18 -06:00
test Added test for correctly finding nested partials, though it will only fail on windows 2016-04-16 08:36:45 -06:00
.editorconfig Add dotfiles 2015-07-25 11:22:36 +02:00
.eslintrc Update eslint config 2015-08-07 16:18:02 +02:00
.gitattributes Add dotfiles 2015-07-25 11:22:36 +02:00
.gitignore first commit 2014-02-04 21:47:35 -08:00
.hound.yml Remove makefile and lint with hound 2016-02-14 14:47:53 +01:00
.travis.yml Remove makefile and lint with hound 2016-02-14 14:47:53 +01:00
History.md 1.6.4 2016-02-22 16:05:45 +01:00
package.json 1.6.4 2016-02-22 16:05:45 +01:00
Readme.md Update readme 2016-02-22 16:09:06 +01:00

metalsmith-layouts

npm version build status dependency status devdependency status downloads

A metalsmith plugin for layouts

stack overflow slack chat

This plugin allows you to apply layouts to your source files. It passes your source files to the selected layout as the variable contents and renders the result with the templating engine of your choice. You can use any templating engine supported by consolidate.js.

For support questions please use stack overflow or our slack channel. For templating engine specific questions try the aforementioned channels, as well as the documentation for consolidate.js and your templating engine of choice.

Installation

$ npm install metalsmith-layouts

Example

Configuration in metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "engine": "handlebars"
    }
  }
}

Source file src/index.html:

---
layout: layout.html
title: The title
---
<p>The contents</p>

Layout layouts/layout.html:

<!doctype html>
<html>
<head>
  <title>{{title}}</title>
</head>
<body>
  {{{contents}}}
</body>
</html>

Results in build/index.html:

<!doctype html>
<html>
<head>
  <title>The title</title>
</head>
<body>
  <p>The contents</p>
</body>
</html>

This is a very basic example. For more elaborate examples see the metalsmith tag on stack overflow.

Options

You can pass options to metalsmith-layouts with the Javascript API or CLI. The options are:

  • engine: templating engine (required)
  • default: default template (optional)
  • directory: directory for the layouts, layouts by default (optional)
  • partials: directory for the partials (optional)
  • pattern: only files that match this pattern will be processed (optional)
  • rename: change the file extension of processed files to .html (optional)

engine

The engine that will render your layouts. Metalsmith-layouts uses consolidate.js to render templating syntax, so any engine supported by consolidate.js can be used. Don't forget to install the templating engine separately. So this metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "engine": "swig"
    }
  }
}

Will render your layouts with swig.

default

The default layout to use. Can be overridden with the layout key in each file's YAML frontmatter, by passing either a layout or false. Passing false will skip the file entirely.

If a default layout has been specified, metalsmith-layouts will process all files unless a pattern has been passed. Don't forget to specify the default template's file extension. So this metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "engine": "swig",
      "default": "default.html"
    }
  }
}

Will apply the default.html layout to all files, unless overridden in the frontmatter.

directory

The directory where metalsmith-layouts looks for the layouts. By default this is layouts. So this metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "engine": "swig",
      "directory": "templates"
    }
  }
}

Will look for layouts in the templates directory, instead of in layouts.

partials

The directory where metalsmith-layouts looks for partials. Each partial is named by removing the file extension from its path (relative to the partials directory), so make sure to avoid duplicates. So this metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "engine": "handlebars",
      "partials": "partials"
    }
  }
}

Would mean that a partial at partials/nav.html can be used in layouts as {{> nav }}, and partials/nested/footer.html can be used as {{> nested/footer }}. Note that passing anything but a string to the partials option will pass the option on to consolidate.

Make sure to check consolidate.js and your templating engine's documentation for guidelines on how to use partials.

pattern

Only files that match this pattern will be processed. So this metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "engine": "swig",
      "pattern": "*.md"
    }
  }
}

Would only process files that have the .md extension.

rename

Change the file extension of processed files to .html (optional). This option is set to false by default. So for example this metalsmith.json:

{
  "plugins": {
    "metalsmith-in-place": {
      "engine": "handlebars",
      "rename": true
    }
  }
}

Would rename the extensions of all processed files to .html.

Consolidate

Any unrecognised options will be passed on to consolidate.js. You can use this, for example, to disable caching by passing cache: false. See the consolidate.js documentation for all options supported by consolidate.

Origins

This plugin is a fork of the now deprecated metalsmith-templates. Splitting up metalsmith-templates into two plugins was suggested by Ian Storm Taylor. The results are:

License

MIT