0.1.0 • Published 2 years ago

@springernature/util-dynamic-partial v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Dynamic Partials

NPM version MIT License

Dynamically render a partial within a handlbars template

Allows us to conditionally reference component handlebars templates by registering a handlebars partial file, and providing a helper for dynamically including that template file.

Install

$ npm install @springernature/util-dynamic-partial

Usage

context.json

{
  "dynamicPartials": {
    "partialName": "./path/to/partialName.hbs"
  }
}

Javascript

const Handlebars = require('handlebars');
const dynamicPartials = require('@springernature/util-dynamic-partial');
const config = require('./context.json');

// Register all dynamic partials
(async () => {
  try {
    await dynamicPartials(Handlebars, config.dynamicPartials);
  } catch (error) {
    console.error(error);
  }
})();

HTML (handlebars)

<h2>Include the dynamic partial below</h2>
{{> partialName }}

Find a more complete example in the demo folder.

API

registerDynamicPartials(Handlebars, dynamicPartials, startingLocation)

Return: Promise Register any number of handlebars partial files as defined within a JSON object

options

Handlebars

Type: Object Required: true The handlebars instance with which to register the partials

dynamicPartials

Type: Object Required: true Key/Value pairs that define the name of the dynamic partial, and the location relative to startingLocation

startingLocation

Type: String Required: true Default: . Path to start looking for the partial templates

License

MIT License © 2021, Springer Nature