+34
-11
lines changedFilter options
+34
-11
lines changed Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ export type RenderOptions = {
23
23
directives?: Object;
24
24
isUnaryTag?: Function;
25
25
cache?: RenderCache;
26
-
template?: string;
26
+
template?: string | (content: string, context: any) => string;
27
27
inject?: boolean;
28
28
basedir?: string;
29
29
shouldPreload?: Function;
@@ -82,14 +82,26 @@ export function createRenderer ({
82
82
}, cb)
83
83
try {
84
84
render(component, write, context, err => {
85
+
if (err) {
86
+
return cb(err)
87
+
}
85
88
if (context && context.rendered) {
86
89
context.rendered(context)
87
90
}
88
91
if (template) {
89
-
result = templateRenderer.renderSync(result, context)
90
-
}
91
-
if (err) {
92
-
cb(err)
92
+
try {
93
+
const res = templateRenderer.render(result, context)
94
+
if (typeof res !== 'string') {
95
+
// function template returning promise
96
+
res
97
+
.then(html => cb(null, html))
98
+
.catch(cb)
99
+
} else {
100
+
cb(null, res)
101
+
}
102
+
} catch (e) {
103
+
cb(e)
104
+
}
93
105
} else {
94
106
cb(null, result)
95
107
}
@@ -119,6 +131,8 @@ export function createRenderer ({
119
131
})
120
132
}
121
133
return renderStream
134
+
} else if (typeof template === 'function') {
135
+
throw new Error(`function template is only supported in renderToString.`)
122
136
} else {
123
137
const templateStream = templateRenderer.createStream(context)
124
138
renderStream.on('error', err => {
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import type { ParsedTemplate } from './parse-template'
11
11
import type { AsyncFileMapper } from './create-async-file-mapper'
12
12
13
13
type TemplateRendererOptions = {
14
-
template: ?string;
14
+
template?: string | (content: string, context: any) => string;
15
15
inject?: boolean;
16
16
clientManifest?: ClientManifest;
17
17
shouldPreload?: (file: string, type: string) => boolean;
@@ -42,7 +42,7 @@ type Resource = {
42
42
export default class TemplateRenderer {
43
43
options: TemplateRendererOptions;
44
44
inject: boolean;
45
-
parsedTemplate: ParsedTemplate | null;
45
+
parsedTemplate: ParsedTemplate | Function | null;
46
46
publicPath: string;
47
47
clientManifest: ClientManifest;
48
48
preloadFiles: Array<Resource>;
@@ -55,8 +55,12 @@ export default class TemplateRenderer {
55
55
this.inject = options.inject !== false
56
56
// if no template option is provided, the renderer is created
57
57
// as a utility object for rendering assets like preload links and scripts.
58
-
this.parsedTemplate = options.template
59
-
? parseTemplate(options.template)
58
+
59
+
const { template } = options
60
+
this.parsedTemplate = template
61
+
? typeof template === 'string'
62
+
? parseTemplate(template)
63
+
: template
60
64
: null
61
65
62
66
// function used to serialize initial state JSON
@@ -89,12 +93,17 @@ export default class TemplateRenderer {
89
93
}
90
94
91
95
// render synchronously given rendered app content and render context
92
-
renderSync (content: string, context: ?Object) {
96
+
render (content: string, context: ?Object): string | Promise<string> {
93
97
const template = this.parsedTemplate
94
98
if (!template) {
95
-
throw new Error('renderSync cannot be called without a template.')
99
+
throw new Error('render cannot be called without a template.')
96
100
}
97
101
context = context || {}
102
+
103
+
if (typeof template === 'function') {
104
+
return template(content, context)
105
+
}
106
+
98
107
if (this.inject) {
99
108
return (
100
109
template.head(context) +
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4