deno.land / x / eta@v3.4.0 / src / render.ts

نووسراو ببینە
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
import { EtaNameResolutionError } from "./err.ts";
/* TYPES */import type { Options } from "./config.ts";import type { TemplateFunction } from "./compile.ts";import type { Eta } from "./core.ts";/* END TYPES */
function handleCache(this: Eta, template: string, options: Partial<Options>): TemplateFunction { const templateStore = options && options.async ? this.templatesAsync : this.templatesSync;
if (this.resolvePath && this.readFile && !template.startsWith("@")) { const templatePath = options.filepath as string;
const cachedTemplate = templateStore.get(templatePath);
if (this.config.cache && cachedTemplate) { return cachedTemplate; } else { const templateString = this.readFile(templatePath);
const templateFn = this.compile(templateString, options);
if (this.config.cache) templateStore.define(templatePath, templateFn);
return templateFn; } } else { const cachedTemplate = templateStore.get(template);
if (cachedTemplate) { return cachedTemplate; } else { throw new EtaNameResolutionError("Failed to get template '" + template + "'"); } }}
export function render<T extends object>( this: Eta, template: string | TemplateFunction, // template name or template function data: T, meta?: { filepath: string }): string { let templateFn: TemplateFunction; const options = { ...meta, async: false };
if (typeof template === "string") { if (this.resolvePath && this.readFile && !template.startsWith("@")) { options.filepath = this.resolvePath(template, options); }
templateFn = handleCache.call(this, template, options); } else { templateFn = template; }
const res = templateFn.call(this, data, options);
return res;}
export function renderAsync<T extends object>( this: Eta, template: string | TemplateFunction, // template name or template function data: T, meta?: { filepath: string }): Promise<string> { let templateFn: TemplateFunction; const options = { ...meta, async: true };
if (typeof template === "string") { if (this.resolvePath && this.readFile && !template.startsWith("@")) { options.filepath = this.resolvePath(template, options); }
templateFn = handleCache.call(this, template, options); } else { templateFn = template; }
const res = templateFn.call(this, data, options);
// Return a promise return Promise.resolve(res);}
export function renderString<T extends object>(this: Eta, template: string, data: T): string { const templateFn = this.compile(template, { async: false });
return render.call(this, templateFn, data);}
export function renderStringAsync<T extends object>( this: Eta, template: string, data: T): Promise<string> { const templateFn = this.compile(template, { async: true });
return renderAsync.call(this, templateFn, data);}
eta

Version Info

Tagged at
2 months ago