summaryrefslogtreecommitdiff
path: root/ext/js/templates/template-renderer.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-04-02 12:42:06 -0400
committerGitHub <noreply@github.com>2021-04-02 12:42:06 -0400
commit8179846e381eb0a87bf3bc266abec8f4400565bc (patch)
treeddb57d7870b461c9758f6de8a25dd76bbfaf81f7 /ext/js/templates/template-renderer.js
parent36b7e34cce776cb09a76c28ce8e78e864dabcdda (diff)
Refactor template rendering (#1583)
* Update _errorToJson to _serializeError * Remove async * Refactor render * Simplify _getModifiedData * Rename data => commonData * Rename templates => template for consistency * Improve errors check * Update tests
Diffstat (limited to 'ext/js/templates/template-renderer.js')
-rw-r--r--ext/js/templates/template-renderer.js43
1 files changed, 24 insertions, 19 deletions
diff --git a/ext/js/templates/template-renderer.js b/ext/js/templates/template-renderer.js
index a7a4a842..5441528c 100644
--- a/ext/js/templates/template-renderer.js
+++ b/ext/js/templates/template-renderer.js
@@ -33,10 +33,21 @@ class TemplateRenderer {
this._dataTypes.set(name, {modifier});
}
- async render(template, data, type) {
+ render(template, data, type) {
+ const instance = this._getTemplateInstance(template);
+ data = this._getModifiedData(data, type);
+ return this._renderTemplate(instance, data);
+ }
+
+ getModifiedData(data, type) {
+ return this._getModifiedData(data, type);
+ }
+
+ // Private
+
+ _getTemplateInstance(template) {
if (!this._helpersRegistered) {
this._registerHelpers();
- this._helpersRegistered = true;
}
const cache = this._cache;
@@ -47,8 +58,11 @@ class TemplateRenderer {
cache.set(template, instance);
}
+ return instance;
+ }
+
+ _renderTemplate(instance, data) {
try {
- data = this._getModifiedData(data, type);
this._stateStack = [new Map()];
return instance(data).trim();
} finally {
@@ -56,27 +70,16 @@ class TemplateRenderer {
}
}
- async getModifiedData(data, type) {
- return this._getModifiedData(data, type);
- }
-
- // Private
-
- _getModifier(type) {
+ _getModifiedData(data, type) {
if (typeof type === 'string') {
const typeInfo = this._dataTypes.get(type);
if (typeof typeInfo !== 'undefined') {
- return typeInfo.modifier;
+ const {modifier} = typeInfo;
+ if (typeof modifier === 'function') {
+ data = modifier(data);
+ }
}
}
- return null;
- }
-
- _getModifiedData(data, type) {
- const modifier = this._getModifier(type);
- if (typeof modifier === 'function') {
- data = modifier(data);
- }
return data;
}
@@ -122,6 +125,8 @@ class TemplateRenderer {
for (const [name, helper] of helpers) {
this._registerHelper(name, helper);
}
+
+ this._helpersRegistered = true;
}
_registerHelper(name, helper) {