Skip to content

Commit 4fab613

Browse files
authoredDec 18, 2024··
fix: STRF-11523 Enable dynamic partials (#1248)
feat: STRF-11523 Enable dynamic partials
1 parent 2604750 commit 4fab613

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed
 

‎lib/template-assembler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from 'graceful-fs';
44
import path from 'path';
55
import upath from 'upath';
66

7-
const partialRegex = /\{\{>\s*([_|\-|a-zA-Z0-9@'"/]+)[^{]*?}}/g;
7+
const partialRegex = /\{\{#?>\s*([_|\-|a-zA-Z0-9@'"/]+)[^{]*?}}/g;
88
const partialWithoutBracketsRegex = /[^>{{}}\s*]+/g;
99
const dynamicComponentRegex = /\{\{\s*?dynamicComponent\s*(?:'|")([_|\-|a-zA-Z0-9/]+)(?:'|").*?}}/g;
1010
const includeRegex = /{{2}>\s*([_|\-|a-zA-Z0-9/]+)[^{]*?}{2}/g;

‎server/plugins/renderer/responses/pencil-response.spec.js

+53-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { jest } from '@jest/globals';
2+
import path from 'path';
3+
import { promisify } from 'util';
24
import PencilResponse from './pencil-response.js';
5+
import templateAssembler from '../../../../lib/template-assembler.js';
36

47
describe('PencilResponse', () => {
58
const assembler = {
6-
getTemplates: (path) => new Promise((resolve) => resolve({ path })),
9+
getTemplates: (p) => new Promise((resolve) => resolve({ path: p })),
710
getTranslations: () => new Promise((resolve) => resolve([])),
811
};
912
let data;
@@ -13,7 +16,10 @@ describe('PencilResponse', () => {
1316
beforeEach(() => {
1417
data = {
1518
context: {
16-
settings: {},
19+
settings: {
20+
base_url: 'http://localhost:3000',
21+
secure_base_url: 'https://localhost:3000',
22+
},
1723
theme_settings: {},
1824
template_engine: 'handlebars-v3',
1925
},
@@ -60,4 +66,49 @@ describe('PencilResponse', () => {
6066
await pencilResponse.respond(request, h);
6167
expect(h.response).toHaveBeenCalledTimes(1);
6268
});
69+
70+
describe('it should successfully render a tempalte with dynamic partials', () => {
71+
it('should render a template with dynamic partials', async () => {
72+
let result = '';
73+
data.template_file = 'pages/page3';
74+
data.context.template_engine = 'handlebars-v4';
75+
76+
h.response = (output) => {
77+
result = output;
78+
return response;
79+
};
80+
const themeAssembler = {
81+
async getTemplates(templatesPath, processor) {
82+
const templates = await promisify(templateAssembler.assemble)(
83+
path.join(process.cwd(), 'test/_mocks/themes/valid', 'templates'),
84+
templatesPath,
85+
);
86+
return processor(templates);
87+
},
88+
getTranslations: async () => {
89+
return {};
90+
},
91+
};
92+
const pencilResponse = new PencilResponse(data, themeAssembler);
93+
await pencilResponse.respond(request, h);
94+
expect(result.content).toEqual(`<!DOCTYPE html>
95+
<html>
96+
<head>
97+
<title>page3.html</title>
98+
99+
</head>
100+
<body>
101+
<h1></h1>
102+
Here is the list:
103+
<ul>
104+
<li>
105+
<a href="item_link_1">Item 1</a>
106+
</li> <li>
107+
<a href="item_link_2">Item 2</a>
108+
</li> <li>
109+
<a href="item_link_3">Item 3</a>
110+
</li><ul></body>
111+
</html>`);
112+
});
113+
});
63114
});

‎test/_mocks/themes/valid/templates/pages/page.3.html ‎test/_mocks/themes/valid/templates/pages/page3.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
<body>
88
<h1>{{theme_settings.customizable_title}}</h1>
99
{{> components/c }}
10-
{{footer.scripts}}
1110
</body>
12-
</html>
11+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.