0.1.3 • Published 1 year ago

migasc-template v0.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

MigascTemplate

Simple and fast templating engine.

For familiarity Mustache open/close syntax is used by default.

Resources

Quickstart

1. Install

npm install migasc-template

2. Import

import MigascTemplate from 'migasc-template';

Or

const MigascTemplate = require('migasc-template');

3. Create Instance

const mt = new MigascTemplate(
  // MigascTemplate Options
  { doAllowWhitespace: true },
  // MigascTemplate Dictionary
  {
    adjective: 'mysterious',
    animal: 'Cats',
    author: 'Sir Walter Scott',
  }
);

4. Compile Templates

Given:

const template =
  '{{ animal }} are a {{ adjective }} kind of folk - {{ author }}';

Use the global dictionary.

mt.compile(template);
// -> Cats are a mysterious kind of folk - Sir Walter Scott

Use a local dictionary.

mt.compile(template, {
  adjective: 'special',
  animal: 'Kevins',
  author: 'Michael Scott',
});
// -> Kevins are a special kind of folk - Michael Scott

Use a precompiled template.

// Precompile the template
mt.setTemplate(template);

mt.template();
// -> Cats are a mysterious kind of folk - Sir Walter Scott

Default Options

OptionTypeDefault
dict404string''
doAllowEscapeCharbooleanfalse
doAllowWhitespacebooleanfalse
maxCharsnumber64
maxWhitespacenumber64
tags.closestring}}
tags.openstring{{
validCharsstringa-zA-Z0-9_-

Example

const mt = new MigascTemplate();

mt.compile('{{animal}} are a {{adjective}} kind of folk - {{author}}', {
  adjective: 'mysterious',
  animal: 'Cats',
  author: 'Sir Walter Scott',
});
// -> Cats are a mysterious kind of folk - Sir Walter Scott

Usage Caution

The default templating language uses Mustache open/close syntax {{variableName}} without any whitespace. MigascTemplate relies on accurate user input as it does not throw template syntax errors. Therefore, it is recommended that templates are written using Mustache/Handlebars syntax highlighting extensions or the Handlebars file extension (.hbs) for syntax feedback.

Generating lists is common usage when using EJS and Handlebars and although this is not the intent of MigascTemplate, it can be achieved via IIFEs.

// Note: This is not efficient code, it's purpose is to showcase
mt.compile('<ul>{{list}}</ul>', {
  list: new Array(20)
    .fill(0)
    .map(() => `<li>${Math.random()}</li>`)
    .join(''),
});

Benchmarks

Benchmark results are produced from the benchmark script.

TestOperations per SecondRelative Margin of ErrorSamples
char 45 EJS.render56,117±0.20%87
char 45 EJS.compile57,228±0.19%98
char 45 EJS.compile (precompiled)475,339±0.25%94
char 45 Handlebars.compile7,717±0.31%94
char 45 Handlebars.compile (precompiled)224,557±0.23%94
char 45 MigascTemplate.compile517,494±0.27%96
char 45 MigascTemplate.template (precompiled)1,003,012±0.22%94
char 380 EJS.render37,654±0.38%99
char 380 EJS.compile38,099±0.17%96
char 380 EJS.compile (precompiled)356,497±0.24%95
char 380 Handlebars.compile7,097±0.37%91
char 380 Handlebars.compile (precompiled)231,864±0.17%92
char 380 MigascTemplate.compile474,601±0.22%98
char 380 MigascTemplate.template (precompiled)1,017,288±0.29%96
char 38022 EJS.render1,597±0.20%98
char 38022 EJS.compile1,600±0.13%96
char 38022 EJS.compile (precompiled)11,765±0.27%96
char 38022 Handlebars.compile231±0.62%83
char 38022 Handlebars.compile (precompiled)60,799±0.96%94
char 38022 MigascTemplate.compile19,123±0.14%96
char 38022 MigascTemplate.template (precompiled)215,903±0.20%96

License

MigascTemplate is released under the MIT license.

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago