@teleology/slim

A tiny javascript template library

Usage no npm install needed!

<script type="module">
  import teleologySlim from 'https://cdn.skypack.dev/@teleology/slim';
</script>

README

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!