From e2b4b078f625f46b1ecff275db70f9f5e0ebf1a9 Mon Sep 17 00:00:00 2001 From: dslak Date: Thu, 29 Sep 2022 13:38:28 +0200 Subject: [PATCH] readme --- README.md | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b586607..6d1d6d0 100644 --- a/README.md +++ b/README.md @@ -1 +1,100 @@ -# staples +# Staples + +Staples is a minimal library that provides the power to allow you to create simple semantic templates and customize them with the addition of custom helpers. Staples is widely compatible with the basic Mustache and Handlebars models. + + + +## Installation + +```bash +npm i @dslak/staples +``` + +or, using YARN + +```bash +yarn add @dslak/staples +``` + + + +## Usage + +Once installed, the library must be included as well + +```javascript +const staples = require('@dslak/staples') +staples.printVersion() +``` + + + +Example of use with a basic template and a test input + +```javascript +const staples = require('@dslak/staples'); + +const input = { + "sections": [ + { + "name": "section1", + "elements": { + "val1": "value 1", + "val2": "value 2", + "val_html": "HTML" + } + }, + { + "name": "section2", + "elements": { + "if": true, + "unless": false + } + } + ] +}; + +const template = `{{sections.0.elements.val1}} + {{sections.0.elements.val2}} +
{{sections.0.elements.val_html}}
+ {{sections.0.elements.val2}} + {{#if sections.1.elements.if}} + {{sections.0.elements.val1}} + {{/if}} + {{#unless sections.1.elements.unless}} + {{sections.0.elements.val2}} + {{/unless}}`; + +const compiled = staples.compile(template, input); +``` + + + +## Custom helpers + +Creating custom helpers + +```javascript +staples.addHelper('ellipsis', (string, words = 10) => { + return `${string.split(' ').slice(0, words).join(' ')}...` +}) +``` + +Usage: + +```javascript +const staples = require('@dslak/staples'); + +staples.addHelper('ellipsis', (string, words = 10) => { + return `${string.split(' ').slice(0, words).join(' ')}...` +}); + +const input = { + "value": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." +}; + +const template = `{{ellipsis value 5}}`; + +const compiled = staples.compile(template, input); +``` +