@teleology/slim
v3.0.6
Published
A tiny javascript template library
Downloads
3
Readme
@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.