2.0.2 • Published 29 days ago

@jakubmazanec/template v2.0.2

Weekly downloads
-
License
LGPL-3.0-only
Repository
github
Last release
29 days ago

@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.

2.0.2

29 days ago

2.0.1-next.1788346

3 months ago

2.0.1-next.60933d4

3 months ago

2.0.1-next.007199a

3 months ago

2.0.1

3 months ago

2.0.1-next.b10d52c

3 months ago

2.0.1-next.01330c0

3 months ago

2.0.1-next.4fd23cd

3 months ago

2.0.0

4 months ago

2.0.0-next.2fa8a2c

4 months ago

2.0.0-next.6fd9f2b

4 months ago

2.0.0-next.b3909a5

4 months ago

2.0.0-next.bc04e4f

4 months ago

2.0.0-next.b19329b

7 months ago

1.0.3-next.121ef1e

9 months ago

1.0.1-next.336fe21

10 months ago

2.0.0-next.f3375f0

7 months ago

1.0.2-186a8e5

10 months ago

1.0.1-6cee770

11 months ago

1.0.1-c2efc51

10 months ago

1.1.0

8 months ago

1.0.3-2487994

10 months ago

1.0.1-7dbb232

10 months ago

1.0.1-dee6883

10 months ago

2.0.0-next.f9ef3d7

7 months ago

2.0.0-next.7a1e391

7 months ago

1.0.3-a1d4551

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.3

9 months ago

1.0.3-b8faa49

9 months ago

1.0.2-next.990a7af

10 months ago

1.0.1-8a0a4d5

10 months ago

1.0.1-2f12db0

10 months ago

2.0.0-next.a956e19

7 months ago

1.1.0-next.5d8abeb

8 months ago

1.0.1-bc209e5

11 months ago

2.0.0-next.7305494

7 months ago

1.0.1-2d1145f

11 months ago

1.0.1-5f6d70c

11 months ago

1.0.1-c5ebb6f

11 months ago

1.0.1-593aae1

11 months ago

1.0.1-265acad

11 months ago

1.0.1-11dd4a3

11 months ago

1.0.1-a500263

11 months ago

1.0.1-9029e49

11 months ago

1.0.0

1 year ago

1.0.0-9cfd7b2

1 year ago

1.0.0-f7731ef

1 year ago

1.0.0-a3e1bca

1 year ago