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

@jakubmazanec/template

v2.0.2

Published

TypeScript-first library for working with templates using EJS and Zod.

Downloads

728

Readme

@jakubmazanec/template

TypeScript-first library for working with templates using EJS and Zod.

Installation

npm install @jakubmazanec/template

⚠️ This is an ESM package! It cannot be required from a CommonJS module.

Prerequisites

  • Node.js 20 or later
  • TypeScript 5 or later

Usage

Write templates using EJS with YAML front matter:

---
extends: './base.ejs'
to: <%- attributes.variables.path %>
if: <%- attributes.variables.enabled %>
variables:
  - path: foo-<%- value %>.json
    enabled: true
  - name: bar-<%- value %>.json
    enabled: false
---
{
    "value": <%- value %>
}

The front matter is used for specifying template attributes that control how is the template rendered. The default attributes can be written in EJS too and are rendered before the template content is rendered.

It is expected that attribute if renders to a string that can be parsed as YAML value; if that value is false, the rendering of the whole template is cancelled.

Attribute to is also rendered and it represents the path of file the resulting template render is supposed to be written into. The extension is also used for file format detection by Prettier.

Attribute variables is an object that is merged with the data object before rendering. If attribute variables is an array of objects, that leads to multiple renders, each with different variables object merged with the data. Each variable that is a string is also rendered.

The JavaScript variable value (used in EJS tags <% %>) is not defined, so it must be passed as a data during the template rendering:

let template = await Template.read('template.ejs', {
  dataSchema: z.object({value: z.number()}) // we have to specify the schema that represents the template data type
});

console.log(await template.render({value: 1}));
// ->
// [{
//   attributes: {
//     to: 'foo-1.json',
//     variables: { path: 'foo-1.json', enabled: true }
//   },
//   data: { value: 1 },
//   content: '{\n  "value": 1\n}\n'
// }]

console.log(await template.render({value: 2}));
// ->
// [{
//   attributes: {
//     to: 'foo-2.json',
//     variables: { path: 'foo-2.json', enabled: true }
//   },
//   data: { value: 2 },
//   content: '{\n  "value": 2\n}\n'
// }]

Because variables is an array, we can expect two renders; but because one of the variables, enabled is used for rendering the value of if attribute and is set to false in the second case, only one render is actually returned.

You can extend another template using extends attribute that represents path to the template to extend, relative to the current template. The extended template is read using the same options object. The current template attributes are recursively merged with the extended template attributes. If the current template has no content, the content from the extended template is used.

Documentation

See API reference for auto-generated documentation.

Contributing

If you want to contribute, see CONTRIBUTING for details.

License

This package is licensed under the GNU Lesser General Public License v3. See LICENSE for details.