Skip to content

Commit fa92d47

Browse files
committed
MAGE-1000 Experiment with template engine adapter
1 parent 1a5e441 commit fa92d47

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

view/frontend/requirejs-config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ var config = {
1616
'additionalHtml' : 'Algolia_AlgoliaSearch/js/template/autocomplete/additional-section',
1717

1818
// Recommend templates
19-
'recommendProductsHtml': 'Algolia_AlgoliaSearch/js/template/recommend/products'
19+
'recommendProductsHtml': 'Algolia_AlgoliaSearch/js/template/recommend/products',
20+
21+
// Unbundling
22+
'algoliaTemplateEngine': 'Algolia_AlgoliaSearch/js/internals/template-engine'
2023
}
2124
},
2225
paths : {

view/frontend/web/js/instantsearch.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ define([
33
'algoliaBundle',
44
'algoliaHoganLib',
55
'algoliaMustacheLib',
6+
'algoliaTemplateEngine',
67
'Magento_Catalog/js/price-utils',
78
'algoliaCommon',
89
'algoliaInsights',
910
'algoliaHooks',
10-
], function ($, algoliaBundle, Hogan, Mustache, priceUtils) {
11+
], function ($, algoliaBundle, Hogan, Mustache, templateEngine, priceUtils) {
12+
1113
const processTemplate = (template, templateVars, useMustache = false) => {
1214
const hoganStart = performance.now();
1315
const wrapperTemplate = Hogan.compile(template);
@@ -26,7 +28,7 @@ define([
2628
return hoganResult;
2729
};
2830

29-
$(function ($) {
31+
$(async function ($) {
3032
/** We have nothing to do here if instantsearch is not enabled **/
3133
if (
3234
typeof algoliaConfig === 'undefined' ||
@@ -127,10 +129,9 @@ define([
127129
config : algoliaConfig.instant,
128130
translations : algoliaConfig.translations,
129131
};
130-
131-
$('.algolia-instant-selector-results')
132-
.html(processTemplate(template, templateVars, true))
133-
.show();
132+
133+
const wrapperHtml = await templateEngine.processTemplate(template, templateVars);
134+
$('.algolia-instant-selector-results').html(wrapperHtml).show();
134135

135136
/**
136137
* Initialise instant search
@@ -373,7 +374,7 @@ define([
373374
$('.algolia-instant-selector-results').show();
374375
}
375376

376-
var template = $('#instant-stats-template').html();
377+
const template = $('#instant-stats-template').html();
377378
return processTemplate(template, data, true);
378379
},
379380
},
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
define([], function () {
2+
const ENGINE_TYPE_HOGAN = 'hogan';
3+
const ENGINE_TYPE_MUSTACHE = 'mustache';
4+
5+
return {
6+
getSelectedEngine: () => ENGINE_TYPE_HOGAN, // override via mixin
7+
8+
processTemplate: async function (template, data, measure = false) {
9+
const adapter = await this.getEngineAdapter(this.getSelectedEngine());
10+
if (measure) {
11+
const start = performance.now();
12+
const result = adapter.process(template, data);
13+
const end = performance.now();
14+
console.log(
15+
`### Template execution time with "${this.getSelectedEngine()}": %s ms`,
16+
end - start
17+
);
18+
return result;
19+
}
20+
21+
return adapter.process(template, data);
22+
},
23+
24+
getEngineAdapter: async function (type) {
25+
switch (type) {
26+
case ENGINE_TYPE_HOGAN:
27+
return this.getHoganAdapter();
28+
case ENGINE_TYPE_MUSTACHE:
29+
return this.getMustacheAdapter();
30+
default:
31+
throw new Error(`Unknown template engine: ${type}`);
32+
}
33+
},
34+
35+
getHoganAdapter: async function () {
36+
const Hogan = await this.getAdapterEngine('algoliaHoganLib');
37+
return {
38+
process: (template, data) => {
39+
return Hogan.compile(template).render(data);
40+
},
41+
};
42+
},
43+
44+
getMustacheAdapter: async function () {
45+
const Mustache = await this.getAdapterEngine('algoliaMustacheLib');
46+
return {
47+
process: (template, data) => {
48+
return Mustache.render(template, data);
49+
},
50+
};
51+
},
52+
53+
getAdapterEngine: async function (lib) {
54+
try {
55+
return await new Promise((resolve, reject) => {
56+
require([lib], resolve, reject);
57+
});
58+
} catch (err) {
59+
console.error(`Failed to load module ${lib} for template engine:`, err);
60+
throw err;
61+
}
62+
},
63+
};
64+
});

0 commit comments

Comments
 (0)