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

js-template-engine

v1.0.1

Published

A dynamic templating engine that translates JavaScript or JSON data into structured templates across multiple languages.

Downloads

3

Readme

JS Template Engine

A dynamic templating engine that translates JavaScript or JSON data into structured templates across multiple languages. At its core this tool generates HTML templates, but the concept is modular and can be extended to render templates for any framework or templating language imaginable.

Features

  • Ideal for UI libraries: Maintain one single source of truth and avoid double maintaining different language variations of your components.
  • Customizable: Does it not yet support your templating language of choice? The abstract logic allows you to create and use your own extensions.
  • Native Extensions: There's a growing ecosystem of extensions, i.e. React to generate JSX components and BEM to enforce consistent class naming.
  • CLI Interface: A convenient CLI tool that can both process single JSON files and traverse through nested folder structures from the command line.
  • Flexible Configuration: Customize the output directory, apply framework-specific extensions, and more through CLI options or configuration files.

Installation

npm install js-template-engine

Or if you prefer using Yarn:

yarn add js-template-engine

Usage

CLI

The JS Template Engine CLI provides a straightforward way to render templates from JSON files:

js-template-engine render <sourcePath> [options]

Arguments:

  • <sourcePath>: The path to the JSON file or directory containing JSON templates you wish to render.

Options:

  • --outputDir, -o: Specify the output directory for rendered templates.
  • --extensions, -e: Choose extensions to use for template processing (e.g., react, bem).
  • --name, -n: Set a base name for output files.
  • --componentName, -c: Define a component name for framework-specific templates (useful for React).
  • --verbose, -v: Enable verbose logging for more detailed output.

Examples

Feel free to check out the examples folder, to get a better idea of some of the core concepts and extensions. The provided examples can be run using:

npm run example:react
npm run example:bem
npm run example:slots

Or if you prefer using Yarn:

yarn example:react
yarn example:bem
yarn example:slots

API

You can also use JS Template Engine programmatically in your Node.js projects. This is how you could define and process your template using the BEM extension:

const { TemplateEngine, BemExtension } = require("js-template-engine");

const templateEngine = new TemplateEngine();
const bemExtension = new BemExtension();

const breadcrumbsTemplate = [
  {
    tag: "nav",
    extensions: {
      bem: {
        block: "breadcrumbs",
      },
    },
    children: [
      {
        tag: "ul",

        extensions: {
          bem: {
            element: "list",
          },
        },
        children: [
          {
            tag: "li",

            extensions: {
              bem: {
                element: "item",
              },
            },
            children: [
              {
                tag: "a",
                extensions: {
                  bem: {
                    element: "text",
                  },
                },
                attributes: {
                  href: "/",
                },
                children: [
                  {
                    type: "text",
                    content: "Home",
                  },
                ],
              },
            ],
          },
          {
            tag: "li",
            extensions: {
              bem: {
                element: "item",
                modifier: "current",
              },
            },
            children: [
              {
                tag: "span",
                extensions: {
                  bem: {
                    element: "text",
                  },
                },
                children: [
                  {
                    type: "text",
                    content: "About",
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
];

templateEngine.render(breadcrumbsTemplate, {
  name: "breadcrumbs",
  extensions: [bemExtension],
});

This is what it would result in:

<nav class="breadcrumbs">
  <ul class="breadcrumbs__list">
    <li class="breadcrumbs__item">
      <a href="/" class="breadcrumbs__text">Home</a>
    </li>
    <li class="breadcrumbs__item breadcrumbs__item--current">
      <span class="breadcrumbs__text">About</span>
    </li>
  </ul>
</nav>

Contributing

Contributions are welcome! Feel free to open pull requests or issues to suggest features, report bugs, or contribute to the code.

Reporting Issues

Found a bug or have a suggestion? Please use the GitHub Issues page to report issues or request features.

License

This project is licensed under the MIT License - see the LICENSE file for details.