deno.land / x / eta@v1.12.3 / render.ts

render.ts
View Documentation
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import compile from "./compile.ts";import { getConfig } from "./config.ts";import { promiseImpl } from "./polyfills.ts";import EtaErr from "./err.ts";
/* TYPES */
import type { EtaConfig, PartialConfig } from "./config.ts";import type { TemplateFunction } from "./compile.ts";import type { CallbackFn } from "./file-handlers.ts";
/* END TYPES */
function handleCache( template: string | TemplateFunction, options: EtaConfig,): TemplateFunction { if (options.cache && options.name && options.templates.get(options.name)) { return options.templates.get(options.name); }
const templateFunc = typeof template === "function" ? template : compile(template, options);
// Note that we don't have to check if it already exists in the cache; // it would have returned earlier if it had if (options.cache && options.name) { options.templates.define(options.name, templateFunc); }
return templateFunc;}
/** * Render a template * * If `template` is a string, Eta will compile it to a function and then call it with the provided data. * If `template` is a template function, Eta will call it with the provided data. * * If `config.async` is `false`, Eta will return the rendered template. * * If `config.async` is `true` and there's a callback function, Eta will call the callback with `(err, renderedTemplate)`. * If `config.async` is `true` and there's not a callback function, Eta will return a Promise that resolves to the rendered template. * * If `config.cache` is `true` and `config` has a `name` or `filename` property, Eta will cache the template on the first render and use the cached template for all subsequent renders. * * @param template Template string or template function * @param data Data to render the template with * @param config Optional config options * @param cb Callback function */
export default function render( template: string | TemplateFunction, data: object, config?: PartialConfig, cb?: CallbackFn,): string | Promise<string> | void { const options = getConfig(config || {});
if (options.async) { if (cb) { // If user passes callback try { // Note: if there is an error while rendering the template, // It will bubble up and be caught here const templateFn = handleCache(template, options); templateFn(data, options, cb); } catch (err) { return cb(err); } } else { // No callback, try returning a promise if (typeof promiseImpl === "function") { return new promiseImpl(function (resolve: Function, reject: Function) { try { resolve(handleCache(template, options)(data, options)); } catch (err) { reject(err); } }); } else { throw EtaErr( "Please provide a callback function, this env doesn't support Promises", ); } } } else { return handleCache(template, options)(data, options); }}
/** * Render a template asynchronously * * If `template` is a string, Eta will compile it to a function and call it with the provided data. * If `template` is a function, Eta will call it with the provided data. * * If there is a callback function, Eta will call it with `(err, renderedTemplate)`. * If there is not a callback function, Eta will return a Promise that resolves to the rendered template * * @param template Template string or template function * @param data Data to render the template with * @param config Optional config options * @param cb Callback function */
export function renderAsync( template: string | TemplateFunction, data: object, config?: PartialConfig, cb?: CallbackFn,): string | Promise<string> | void { // Using Object.assign to lower bundle size, using spread operator makes it larger because of typescript injected polyfills return render(template, data, Object.assign({}, config, { async: true }), cb);}
eta
Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
DocumentationDocumentation
GitHub Repositoryeta-dev/eta
GitHub Stars
527

Version Info

Tagged at
10 months ago

External Dependencies

1 external dependency