Détail du package

metalsmith-layouts

metalsmith23.1kMITobsolète2.3.1

This package is no longer maintained and has moved to the @metalsmith org; Please migrate to the following package: @metalsmith/layouts

A metalsmith plugin for layouts.

readme

metalsmith-layouts

build status coverage status greenkeeper

A metalsmith plugin for layouts

This plugin allows you to wrap your files in a template (a layout) and abstract repetitive html. The plugin will pass the contents of your files as the variable contents and renders the result with the appropriate engine. It uses the file extension of your layout to infer which templating engine to use. So layouts with names ending in .njk will be processed as nunjucks, .hbs as handlebars, etc.

If you want to process templating syntax in your files, instead of wrapping them in a template, you can use metalsmith-in-place. 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 jstransformers and your templating engine of choice.

How does it work

Under the hood this plugin uses jstransformers to render your layouts. Since there are over a 100 jstransformers we don't install them automatically, so you'll need to install the jstransformer for the language you want to use.

For example, to render nunjucks you would install jstransformer-nunjucks, to render handlebars you would install jstransformer-handlebars, etc. The plugin will then automatically detect which jstransformers you've installed. See the jstransformer organisation for all available jstransformers and this dictionary to see which extensions map to which jstransformer.

Installation

$ npm install metalsmith-layouts

Options

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

  • default: optional. The default layout to apply to files.
  • directory: optional. The directory for the layouts. The default is layouts.
  • pattern: optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is **.
  • engineOptions: optional. Use this to pass options to the jstransformer that's rendering your layouts. The default is {}.
  • suppressNoFilesError: optional. An error won’t be thrown if no files are present for processing.

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 apply layouts to all files, so you might want to ignore certain files with a pattern. Don't forget to specify the default template's file extension. So this metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "default": "default.hbs"
    }
  }
}

Will apply the default.hbs 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": {
      "directory": "templates"
    }
  }
}

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

pattern

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

{
  "plugins": {
    "metalsmith-layouts": {
      "pattern": "**/*.html"
    }
  }
}

Would process all files that have the .html extension. Beware that the extensions might be changed by other plugins in the build chain, preventing the pattern from matching. We use multimatch for the pattern matching.

engineOptions

Use this to pass options to the jstransformer that's rendering your templates. So this metalsmith.json:

{
  "plugins": {
    "metalsmith-layouts": {
      "engineOptions": {
        "cache": false
      }
    }
  }
}

Would pass { "cache": false } to the used jstransformer.

suppressNoFilesError

metalsmith-layouts throws an error in metalsmith if it can’t find any files to process. If you’re doing any kind of incremental builds via something like metalsmith-watch, this is problematic as you’re likely only rebuilding files that have changed. This flag allows you to suppress that error.

Note that if you have debugging turned on, you’ll see a message denoting when no files are present for processing.

Example

1. Install dependencies:

$ npm install --save metalsmith metalsmith-layouts

In this case we'll use handlebars, so we'll install jstransformer-handlebars:

$ npm install --save jstransformer-handlebars

2. Configure metalsmith

We'll create a metalsmith.json configuration file in the root of the project, a file in ./src that we want to render in a layout and a handlebars layout for metalsmith-layouts to process in ./layouts:

./metalsmith.json

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-layouts": true
  }
}

./src/index.html

---
title: The title
layout: layout.hbs
---
<p>Some text here.</p>

./layouts/layout.hbs

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

3. Build

To build just run the metalsmith CLI:

$ node_modules/.bin/metalsmith

Which will output the following file:

./build/index.html

<!DOCTYPE html>
<html>
  <head>
    <title>The title</title>
  </head>
  <body>
    <p>Some text here.</p>
  </body>
</html>

FAQ

I want to use handlebars partials and or helpers.

Use metalsmith-discover-partials and metalsmith-discover-helpers.

I want to change the extension of my templates.

Use metalsmith-rename.

My templating language requires a filename property to be set.

Use metalsmith-filenames.

Errors and debugging

If you're encountering problems you can use debug to enable verbose logging. To enable debug prefix your build command with DEBUG=metalsmith-layouts. So if you normally run metalsmith to build, use DEBUG=metalsmith-layouts metalsmith (on windows the syntax is slightly different).

No files to process

There are several things that might cause you to get a no files to process error:

  • Your pattern does not match any files
  • None of your files pass validation, validation fails for files that:
    • Have no layout
    • Have a layout without an extension
    • Are not utf-8
    • Have a layout that needs a jstransformer that hasn't been installed

Credits

License

MIT

changelog

2.3.1 - April 7, 2019

  • prefix rendering errors with filename that caused the error

2.3.0 - November 1, 2018

  • add support for async rendering

2.2.0 - September 4, 2018

  • add suppressNoFilesError feature

2.1.0 - January 25, 2018

  • add a documentation link to the error messages
  • add debug for better debugging

2.0.0 - January 10, 2018

  • moved to jstransformers (and dropped consolidate)
  • breaking: dropped node 0.12 and node 4 support
  • breaking: removed options:
    • engine: inferred automatically from the layout extension by jstransformer
    • partials: use metalsmith-discover-partials (and metalsmith-discover-helpers) instead
    • partialExtension: no longer necessary
    • rename: use metalsmith-rename instead
    • exposeConsolidate: no longer necessary

1.8.1 - April 20, 2017

  • fix "too many opened files" error

1.8.0 - February 3, 2017

  • add partialExtension option, to fix partials matching on the wrong extension

1.7.0 - November 6, 2016

  • expose consolidate.requires fix (#18)
  • check if paths are already absolute before joining the metalsmith path

1.6.5 - May 3, 2016

  • normalize partial name for windows

1.6.4 - February 22, 2016

  • allow layout: false to override default template

1.5.4 - February 14, 2016

  • add rename option
  • prevent path issue on windows

1.4.4 - January 27, 2016

  • update lodash.omit

1.4.3 - January 27, 2016

  • update consolidate

1.4.2 - October 17, 2015

  • update fs-readdir-recursive

1.4.1 - September 19, 2015

  • move check for unspecified layout, #41
  • update devdependencies

1.4.0 - August 14, 2015

  • ignore files with unspecified layout
  • update readme

1.3.1 - August 8, 2015

  • improve error message for unspecified layout
  • update readme

1.3.0 - August 7, 2015

  • add swig include test
  • code style, dependency and readme updates
  • add partials option and test

1.2.1 - July 25, 2015

  • add error handling
  • add test for partials

1.2.0 - July 25, 2015

  • ignore binary files
  • add dotfiles
  • update readme

1.1.0 - July 19, 2015

  • update dependencies and devDependencies

1.0.1 - July 16, 2015

  • update tests and add Travis CI

1.0.0 - November 18, 2014

  • remove inPlace option
  • use layout instead of template in the front-matter to specify a layout
  • set default folder for layouts to layouts instead of templates

0.6.0 - October 3, 2014

  • fix to use path for metalsmith 1.0.0

0.5.2 - July 9, 2014

  • fix breaking binary files

0.5.1 - June 11, 2014

  • fix race condition with stringify file contents

0.5.0 - April 29, 2014

  • pass in options to consolidate.js

0.4.0 - April 2, 2014

  • add default option

0.3.0 - March 10, 2014

  • add inPlace option
  • change pattern option to just filter

0.2.1 - March 10, 2014

  • fix bug in matching pattern logic

0.2.0 - March 8, 2014

  • add rendering files in place with a pattern

0.1.0 - March 5, 2014

  • add string engine convenience

0.0.6 - February 7, 2014

  • fix directory option bug

0.0.5 - February 7, 2014

  • stringify contents on the original file data

0.0.4 - February 6, 2014

  • switch to extend from defaults to avoid cloning
  • add debug statements

0.0.3 - February 6, 2014

  • fix to use buffers

0.0.2 - February 5, 2014

  • mix in metadata

0.0.1 - February 4, 2014

:sparkles: