@teleology/slim
TypeScript icon, indicating that this package has built-in type declarations

3.0.6 • Public • Published

npm version npm license

@teleology/slim

A tiny javascript template rendering engine

Installation

npm i -D @teleology/slim

or

yarn add -D @teleology/slim

Usage

Anything within {{ }} will be treated as javascript. Whitespace will be ignored. In the advent that a function is referenced without any parameters, it will be invoked with the context object.

Example:

const { slim } = require('@teleology/slim');

const template = `
    Hey {{ user.name }}, how are you doing?? 
    Haven't seen you since the vacation in {{ user.lastKnownLocation }}.
`;

const context = {
    user: {
        name: 'McTester',
        lastKnownLocation: 'Japan'
    },
};

console.log(slim(template, context));

Output:

    Hey McTester, how are you doing?? 
    Haven't seen you since the vacation in Japan.

Parameter Injection

Example:

const context = {
  a: 'New York',
  z: 'Spain',
}

const template = `From {{ a }} to {{ z }}.`;

slim(template, context); 

Output:

From New York to Spain.

Regular Function

Example:

const context = {
  add: (a, b, c) => a + b + c,
}

const template = `Addition: {{ add(2, '3', 1.4) }}`;

slim(template, context); 

Output:

Addition: 231.4

Contextual Function Params

Example:

const context = {
  bikeWheels: (a, b) => a * b,
  BIKE_TIRES: 2,
}

const template = `All 3 of them had, {{ bikeWheels(3, BIKE_TIRES) }} tires!`;

slim(template, context); 

Output:

All 3 of them had, 6 tires!

Experimental

An experimental async functionality is included in this build. This will allow async functions to be used within the template.

const { slimAsync } = require('@teleology/slim');

const template = `
    Hey {{ user.name }}, how are you doing?? 
    Haven't seen you since the vacation in {{ lastKnownLocation }}.
`;

const context = {
  user: {
    name: 'McTester',
  },
  lastKnownLocation: async () =>
    new Promise((res) => {
      res('Japan');
    }),
};

(async () => {
  console.log(await slimAsync(template, context));
})();

Output:

    Hey McTester, how are you doing?? 
    Haven't seen you since the vacation in Japan.

Readme

Keywords

Package Sidebar

Install

npm i @teleology/slim

Weekly Downloads

9

Version

3.0.6

License

MIT

Unpacked Size

16.2 kB

Total Files

33

Last publish

Collaborators

  • icarus-sullivan