Giter VIP home page Giter VIP logo

metalsmith-handlebars-x's Introduction

Metalsmith+handlebars

The best plugin for metalsmith + Handlebars. Global/local partials, in-place & layout compilation, and maximum customizability.

Combines all of metalsmith-handlebars, metalsmith-handlebars-contents, metalsmith-handlebars-layouts, metalsmith-discover-helpers, metalsmith-discover-partials, @goodthnx/metalsmith-handlebars, metalsmith-nested, metalsmith-register-helpers, metalsmith-register-partials and adds more. Written out of frustration with incomplete and/or complex existing options.

metalsmith: plugin npm: version travis: build code coverage license: LGPL-3.0

Features

  • filter files by pattern
  • auto-load global and local partials found in directory specified by option partials
  • compile contents (like metalsmith-in-place) and layout (like metalsmith-layouts)
  • includes useful basic set and call helpers
  • allows custom Handlebars, partials, helpers
  • also works with metalsmith-layouts, handlebars-layouts

Install

NPM:

npm i -D metalsmith-handlebars-x

Yarn:

yarn add metalsmith-handlebars-x

Quickstart

var xhandlebars = require('metalsmith-handlebars-x');

// use defaults
metalsmith.use(xhandlebars());

// use defaults (explicit)
metalsmith.use(xhandlebars({
  instance: require('handlebars'),
  pattern: '**/*.{hbs,handlebars}',
  layout: true,
  partials: 'partials',
  helpers: {},
  context: (filemeta, globalmeta) => Object.assign({}, globalmeta, filemeta),
  renameExtension: null
});

Debug

Set env var DEBUG to metalsmith-handlebars-x.

Linux / OSX

DEBUG=metalsmith-handlebars-x

Windows

set DEBUG=metalsmith-handlebars-x

Usage

Partials

Partials will be looked for in the directory specified in the partials option. Partials are automatically removed from the build output unless layout: false. A partial at partials/blockquote.hbs will be usable in templates as blockquote.
A partial at partials/layout/default.hbs will be usable in templates as layout/default.

Global partials

Global partials (available to all templates) can be registered either by specifying a directory in the Metalsmith.source directory:

metalsmith.use(handlebars({ partials: 'partials' }));

or by passing the Handlebars instance to the plugin:

var Handlebars = require('handlebars');
Handlebars.registerPartial('blockquote', '<blockquote cite="{{url}}">{{ quote }}</blockquote>');

metalsmith.use(handlebars({ instance: Handlebars }));

Both can be used together!

Local partials

Local partials will be available to all templates that reside in the same directory.
In the directory structure below both .md files have access to partial layout/default.

├── partials
|    └── layout
|         └── default.hbs
└── posts
    ├── partials
    |    └── local.hbs
    ├── subfolder
    |    └── post-without-local-partials.md
    └── post-with-local-partials.md

Helpers

metalsmith-handlebars-x provides 2 useful helpers by default:

  • call : {{ call func arg1 arg2 arg3 }} allows calling functions available in template context
  • set: {{ set varname value }} allows storing temporary variables for re-use in the template

Register extra helpers through the helpers option as {[helperName]: helperFunc}, or similar to global partials, register them directly on a Handlebars instance passed to the instance option.

Metadata mapping

Specify a custom context option to map file & global metadata in your templates, e.g:

context: function(filemeta, globalmeta) {
  return {
    page: filemeta,
    site: globalmeta
  };
}

In your templates:

{{ site.sitename }} {{ page.stats.birthTime }}

File extensions

With the renameExtension option you can choose what to do with a file's extension:

  • null or undefined does nothing (default)
  • "" (empty string) removes the last extension (so index.hbs would become index, while style.css.hbs would become style.css)
  • ".<ext>" renames the last extension to <ext>, for example index.hbs would become index.html if renameExtension: '.html'.

Usage with handlebars-layouts

Relatively straight-forward:

const Handlebars = require('handlebars');
const hbsLayouts = require('handlebars-layouts')(Handlebars);
const xhandlebars = require('metalsmith-handlebars-x');

Handlebars.registerHelper(hbsLayouts);

metalsmith.use(xhandlebars({ instance: Handlebars }));

With @metalsmith/layouts & metalsmith-discover-partials

You can use this plugin together with @metalsmith/layouts & metalsmith-discover-partials but you should specify layout: false in order to avoid conflicts. metalsmith-handlebars-x will then only compile file contents. See test.js for an example.

License

LGPL v3

metalsmith-handlebars-x's People

Contributors

dependabot[bot] avatar webketje avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

metalsmith-handlebars-x's Issues

Can't get it working

Hi webketje,

i have copied the content of the folder 'test/mocks' to a new node project under 'src'.

index.js:

const Metalsmith = require('metalsmith'),
    xhandlebars = require('metalsmith-handlebars-x'),
    Handlebars = require('handlebars'),
    layouts = require('metalsmith-layouts'),
    discover = require('metalsmith-discover-partials');

// Handlebars.registerHelper(require('handlebars-layouts')(Handlebars));

Metalsmith(__dirname)
    .source('./src')
    .destination('./dist')
    .clean(true)
    .use(discover({
        directory: './partials'
    }))
    .use(layouts({
        directory: './partials',
        pattern: '**/test.hbs',
        suppressNoFilesError: true
    }))
    .ignore('**/handlebars-layouts*')
    .use(xhandlebars({
        instance: Handlebars,
        pattern: '**/*.{hbs}',
        layout: true,
        partials: 'partials',
        helpers: {},
    }))
    .process(function(err, result) {
        if (err) throw err;

        Object.keys(result).forEach((prop) => {
            console.log(prop);
            console.log(result[prop].contents.toString());
            console.log();
        });
    });

package.json:

{
  "name": "metal-test",
  "version": "1.0.0",
  "description": "test metalsmith",
  "main": "metalsmith.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "handlebars": "^4.7.6",
    "handlebars-layouts": "^3.1.4",
    "metalsmith": "^2.3.0",
    "metalsmith-discover-partials": "^0.1.2",
    "metalsmith-handlebars-x": "^0.2.2",
    "metalsmith-layouts": "^2.3.1"
  },
  "dependencies": {}
}

But the output of my script is always:

test.hbs
 {{>include}}

posts/test.hbs
 {{>include}}

I can't figure out what i'm doing wrong.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.