mikel 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +76 -12
  2. package/index.js +12 -5
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  ![npm version](https://badgen.net/npm/v/mikel?labelColor=1d2734&color=21bf81)
4
4
  ![license](https://badgen.net/github/license/jmjuanes/mikel?labelColor=1d2734&color=21bf81)
5
5
 
6
- Mikel is a lightweight implementation of the Mustache templating system in JavaScript, designed to be concise and simple. With less than 60 lines of code, it offers a convenient way to render templates using data.
6
+ Mikel is a lightweight templating library based on the [Mustache](https://mustache.github.io) syntax, designed to be concise and easy to use. It provides a simple way to render templates using data objects, supporting features such as variables, partials, conditional sections, and looping. With a focus on simplicity and minimalism, Mikel offers a tiny yet powerful solution for generating dynamic content in JavaScript applications.
7
7
 
8
8
  ## Installation
9
9
 
@@ -17,36 +17,100 @@ $ npm install mikel
17
17
  $ yarn add mikel
18
18
  ```
19
19
 
20
- ## Usage
20
+ ## Syntax
21
21
 
22
- ### `m(template, data)`
22
+ Mikel supports the following syntax for rendering templates:
23
+
24
+ ### Variables
25
+
26
+ Use double curly braces `{{ }}` to insert variables into your template. Variables will be replaced with the corresponding values from the data object.
27
+
28
+ ### Sections
29
+
30
+ Sections allow for conditional rendering of blocks of content based on the presence or absence of a value in the data object. Use the pound symbol `#` to start a section and the caret `^` to denote an inverted section. End the section with a forward slash `/`.
31
+
32
+ Example:
33
+
34
+ ```javascript
35
+ const data = {
36
+ isAdmin: true,
37
+ };
38
+ const result = m("{{#isAdmin}}You are Admin{{/isAdmin}}", data);
39
+ // Output: 'You are Admin'
40
+ ```
41
+
42
+ You can also use sections for looping over arrays. When looping over array of strings, you can use a dot `.` to reference the current item in the loop.
43
+
44
+ Example:
45
+
46
+ ```javascript
47
+ const data = {
48
+ users: [
49
+ { name: "John" },
50
+ { name: "Alice" },
51
+ { name: "Bob" }
52
+ ],
53
+ };
54
+
55
+ const result = m("Users:{{# users }} {{ name }},{{/ users }}", data);
56
+ // Output: 'Users: John, Alice, Bob,'
57
+ ```
58
+
59
+ Inverted sections render their block of content if the value is falsy or the key does not exist in the data object.
60
+
61
+ Example:
62
+
63
+ ```javascript
64
+ const data = {
65
+ isAdmin: false,
66
+ };
67
+ const result = m("{{^isAdmin}}You are not Admin{{/isAdmin}}", data);
68
+ // Output: 'You are not Admin'
69
+ ```
70
+
71
+ ### Partials (added in v0.3.0)
72
+
73
+ Partials allow you to include separate templates within your main template. Use the greater than symbol `>` followed by the partial name inside double curly braces `{{> partialName }}`.
74
+
75
+ Example:
76
+
77
+ ```javascript
78
+ const data = {
79
+ name: "Bob",
80
+ };
81
+
82
+ const partials = {
83
+ hello: "Hello {{name}}!",
84
+ };
85
+
86
+ const result = m("{{> hello}}", data, {partials});
87
+ // Output: 'Hello Bob!'
88
+ ```
89
+
90
+ ## API
91
+
92
+ ### `m(template, data[, options])`
23
93
 
24
94
  Render the given template string with the provided data object.
25
95
 
26
96
  - `template` (string): The Mustache template string.
27
97
  - `data` (object): The data object containing the values to render.
98
+ - `options` (object): An object containing the following optional values:
99
+ - `partials` (object): An object containing the available partials.
28
100
 
29
101
  Returns: A string with the rendered output.
30
102
 
31
103
  ```javascript
32
104
  import m from "mikel";
33
105
 
34
- const template = "Hello, {{ name }}!";
35
106
  const data = {
36
107
  name: "World",
37
108
  };
38
109
 
39
- const result = m(template, data);
110
+ const result = m("Hello, {{name}}!", data);
40
111
  console.log(result); // Output: "Hello, World!"
41
112
  ```
42
113
 
43
- ## Limitations
44
-
45
- Mikel aims to provide a minimalistic implementation of Mustache templating and currently has the following limitations:
46
-
47
- - **No support for partials:** Mikel does not support partials, meaning you cannot include separate templates within your main template file.
48
- - **Basic functionality:** While Mikel supports the core features of Mustache templating, it may lack some advanced features found in other implementations.
49
-
50
114
  ## License
51
115
 
52
116
  This project is licensed under the [MIT License](LICENSE).
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ const tags = /\{\{|\}\}/;
1
2
  const escapedChars = {
2
3
  "&": "&",
3
4
  "<": "&lt;",
@@ -14,7 +15,7 @@ const get = (ctx, path) => {
14
15
  return path === "." ? ctx : (path.split(".").reduce((p, k) => p?.[k], ctx) || "");
15
16
  };
16
17
 
17
- const compile = (tokens, output, ctx, index, section) => {
18
+ const compile = (tokens, output, ctx, opt, index, section) => {
18
19
  let i = index;
19
20
  while (i < tokens.length) {
20
21
  if (i % 2 === 0) {
@@ -30,12 +31,18 @@ const compile = (tokens, output, ctx, index, section) => {
30
31
  if (!negate && value && Array.isArray(value)) {
31
32
  const j = i + 1;
32
33
  value.forEach(item => {
33
- i = compile(tokens, output, item, j, t);
34
+ i = compile(tokens, output, item, opt, j, t);
34
35
  });
35
36
  }
36
37
  else {
37
38
  const includeOutput = (!negate && !!value) || (negate && !!!value);
38
- i = compile(tokens, includeOutput ? output : [], ctx, i + 1, t);
39
+ i = compile(tokens, includeOutput ? output : [], ctx, opt, i + 1, t);
40
+ }
41
+ }
42
+ else if (tokens[i].startsWith(">")) {
43
+ const t = tokens[i].slice(1).trim();
44
+ if (typeof opt?.partials?.[t] === "string") {
45
+ compile(opt.partials[t].split(tags), output, ctx, opt, 0, "");
39
46
  }
40
47
  }
41
48
  else if (tokens[i].startsWith("/")) {
@@ -52,7 +59,7 @@ const compile = (tokens, output, ctx, index, section) => {
52
59
  return i;
53
60
  };
54
61
 
55
- export default (str, ctx = {}, output = []) => {
56
- compile(str.split(/\{\{|\}\}/), output, ctx, 0, "");
62
+ export default (str, ctx = {}, opt = {}, output = []) => {
63
+ compile(str.split(tags), output, ctx, opt, 0, "");
57
64
  return output.join("");
58
65
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel",
3
- "description": "Micro mustache implementation with zero dependencies",
4
- "version": "0.2.1",
3
+ "description": "Micro templating library with zero dependencies",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Josemi Juanes",