npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lb.generator.handlebars

v1.2.19

Published

Template code generator based on Handlebars

Downloads

57

Readme

lb.generator.handlebars

The LB.Generator.Handlebars NPM package provides a template code generation library based on the Handlebars template engine. The library is easily installed via npm and provides a set of tools to generate any text-based asset from a JSON datasource.

Installation

npm install --save lb.generator.handlebars

Test

node generate.js

Usage


1. Include the library

const Generator = require('lb.generator.handlebars');

2. Load or define a model

// Load a model
const model = require("./sample-templates/sample-model.json");

// Or

// Define a model
const model = {
    Description: "Test Model",
    Items: [
        { 
            Name: "ItemA", 
            Description: "Item - A", 
            Options: [ 
                { Id: "A", Description: "Option A" },
                { Id: "B", Description: "Option B" }
            ] 
        },
        { 
            Name: "ItemB", 
            Description: "Item - B", 
            Options: [ 
                { Id: "C", Description: "Option C" },
                { Id: "D", Description: "Option D" }
            ] 
        }
    ]
};

3. Define a template

Create a Handlebars template file: ./sample-templates/sample.hbs

This sample template is generated for each item in model.Items.

You have access to the entire model:
  Model Description: {{model.Description}}

You have access to the current scoped item in the Items array:
  Item Name: {{item.Name}}
    Description: {{item.Description}}
    Options:
      {{#each item.Options}}
        Id: {{Id}}  Description: {{Description}}
      {{/each}}

4. Define the template settings

Create a template settings file: ./sample-templates/sample.hbs.settings.json

{
    "Target": "Items",
    "TargetItem": "item",
    "ExportPath": ".\\Generated\\Items\\\\{{item.Name}}.txt",
    "AppendToExisting": false
}

Note: When using backslashes in a path, you must escape the backslashes an additional time if it precedes a Handldebars expression


5. Create a Template Loader

Create a Template Loader and pass it the path to the directory containing the templates. The loader will automatically load all template files ending in ".hbs" and their corresponding settings ".hbs.settings.json"

var loader = new Generator.TemplateLoader('./sample-templates');

6. Load and generate the templates

  1. Call the loader.load() function and pass a callback that recieves an array of loaded templates.
  2. Iterate the loaded template array and call template.generate() for each template passing in your model.
  3. Call template.write() on a generated template to write the generated template to its defined file.
// Load the templates
loader.load();

// Generate the loaded templates.
loader.generate(model, (loader) => { console.log("Templates Generated."); });

Or

// Load and generate
loader.loadAndGenerate(model, (loader) => { console.log('Templates loaded and generated.'); });

Or

// Load the templates with a callback containing the list of loaded templates
loader.load(function (templates) {
    // Interate the templates and generate each with the supplied model
    for (const template of templates) {
        console.log(`Generating template: ${template.name}`)
        // Generate the template
        template.generate(model);
        // Write the generated template to file
        template.write();
    }
});