-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfromMustacheTemplate.ts
More file actions
73 lines (67 loc) · 1.73 KB
/
Copy pathfromMustacheTemplate.ts
File metadata and controls
73 lines (67 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { DefaultState } from "../utils/state"
import Mustache from "mustache"
export { default as defaultMustacheTemplate } from "./autocomplete.mustache"
export type Options = {
/**
* Mustache helpers to extend template functionality.
*/
helpers?: object
}
/**
* Render a Mustache template into a container
*
* @param template Mustache template
* @param options Options object.
* @returns Render function
* @group Autocomplete
* @category Mustache
/**
* @example
* ```js
* import { fromMustacheTemplate } from "@nosto/autocomplete/mustache";
*
* const render = fromMustacheTemplate(`
* <div>
* <h1>{{title}}</h1>
* <ul>
* {{#products}}
* <li>{{name}}</li>
* {{/products}}
* </ul>
* </div>
* `, {
* helpers: {
* toJson: function () {
* return JSON.stringify(this)
* }});
*
* render(document.getElementById("container"), {
* title: "My Title",
* products: [
* { name: "Product 1" },
* { name: "Product 2" },
* { name: "Product 3" }
* ]
* });
* ```
*/
export function fromMustacheTemplate<State extends object = DefaultState>(template: string, options?: Options) {
if (Mustache === undefined) {
throw new Error("Mustache is not defined. Please include the Mustache dependency or library in your page.")
}
const { helpers } = options || {}
return (container: HTMLElement, state: State) => {
container.innerHTML = Mustache.render(template, {
...state,
imagePlaceholder: "https://cdn.nosto.com/nosto/9/mock",
toJson: function () {
return JSON.stringify(this)
},
showListPrice: function () {
return this.listPrice !== this.price
},
...helpers
})
return Promise.resolve()
}
}