2.2.9 • Published 4 years ago

json-template-engine v2.2.9

Weekly downloads
58
License
MIT
Repository
github
Last release
4 years ago

Install

npm install json-template-engine

Usage

const template = {
  simpleText: "bar",
  condition: {
    command: "if",
    inputs: [
      {
        name:"condition",
        value: "{{condition.enable}}"
      }
    ],
    outputs: [
      {
        name: "then",
        template: { data: "condition then" }
      },
      {
        name: "else",
        template: { data: "condition else" }
      }
    ]
  },
  each: {
    command: "each",
    inputs: [
      {
          name: "values",
          value: "#range(3, 0)"
      }
    ],
    outputs: {
      name: "iteration",
      template: "value: {{iteration.value}}; index: {{iteration.index}}"
    }
  }
};
const data = {
  condition: {
    enable: false
  }
};

const JSONTemplateEngine = require("json-template-engine");
const JsonParser = new JSONTemplateEngine({
 keyHelper: "command" // change key for helper, default 'command'
});

JsonParser.compile(template, data).then(result => {
  console.log(result);
});

/* print result
    {
      simpleText: 'bar',
      condition: { data: 'condition else' },
      each: [ 'value: 0; index: 0', 'value: 1; index: 1', 'value: 2; index: 2' ]
    }
*/

Helpers

In the library there is the possibility of implementing 2 types of helpers.

  1. Helpers for changing the structure of templates. Out of the box there is an "if" and "each" helper.
  2. Function helpers (asynchronous functions are also supported). The default range helper is available, which generates an array of elements.

You can register by calling the methods JsonParser.registerHelper (<directive>, <handler>) and JsonParser.registerFunctionHelper (<directive>, <handler>), respectively.

Example

For example, I will give registration of helpers "range" and "if".

const ifHelper = async (inputs, outputs, data, utils) => {
    const condition = inputs.find(input => input.name === "condition");
    if (condition === undefined || outputs === undefined) {
       return undefined;
    }
    const outputName = condition.value ? "then" : "else";
    const output = outputs.find(e => e.name === outputName);
    if (output) {
      return await utils.parse(output.template, data);
    }
    return undefined;
};

const rangeFunctionHelper = async (size = 0, startAt = 0) => {
  return Array.from(Array(size).keys()).map(i => i + startAt);
};

const JSONTemplateEngine = require("json-template-engine");
const JsonParser = new JSONTemplateEngine();
JsonParser.registerHelper("if", ifHelper); // The name of the helper must begin with the symbol "#".
JsonParser.registerFunctionHelper("range", rangeFunctionHelper); // The name of the helper must begin with the symbol "#".
2.2.7

4 years ago

2.2.6

4 years ago

2.2.9

4 years ago

2.2.8

4 years ago

2.2.5

4 years ago

2.2.4

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

5 years ago

2.1.6

5 years ago

2.1.7

5 years ago

2.1.5

5 years ago

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago