diff --git a/lib/template-assembler.js b/lib/template-assembler.js index a77d19a5..81508749 100644 --- a/lib/template-assembler.js +++ b/lib/template-assembler.js @@ -4,7 +4,7 @@ import fs from 'graceful-fs'; import path from 'path'; import upath from 'upath'; -const partialRegex = /\{\{>\s*([_|\-|a-zA-Z0-9@'"/]+)[^{]*?}}/g; +const partialRegex = /\{\{#?>\s*([_|\-|a-zA-Z0-9@'"/]+)[^{]*?}}/g; const partialWithoutBracketsRegex = /[^>{{}}\s*]+/g; const dynamicComponentRegex = /\{\{\s*?dynamicComponent\s*(?:'|")([_|\-|a-zA-Z0-9/]+)(?:'|").*?}}/g; const includeRegex = /{{2}>\s*([_|\-|a-zA-Z0-9/]+)[^{]*?}{2}/g; diff --git a/server/plugins/renderer/responses/pencil-response.spec.js b/server/plugins/renderer/responses/pencil-response.spec.js index 5e98f8d5..2e4d5829 100644 --- a/server/plugins/renderer/responses/pencil-response.spec.js +++ b/server/plugins/renderer/responses/pencil-response.spec.js @@ -1,9 +1,12 @@ import { jest } from '@jest/globals'; +import path from 'path'; +import { promisify } from 'util'; import PencilResponse from './pencil-response.js'; +import templateAssembler from '../../../../lib/template-assembler.js'; describe('PencilResponse', () => { const assembler = { - getTemplates: (path) => new Promise((resolve) => resolve({ path })), + getTemplates: (p) => new Promise((resolve) => resolve({ path: p })), getTranslations: () => new Promise((resolve) => resolve([])), }; let data; @@ -13,7 +16,10 @@ describe('PencilResponse', () => { beforeEach(() => { data = { context: { - settings: {}, + settings: { + base_url: 'http://localhost:3000', + secure_base_url: 'https://localhost:3000', + }, theme_settings: {}, template_engine: 'handlebars-v3', }, @@ -60,4 +66,49 @@ describe('PencilResponse', () => { await pencilResponse.respond(request, h); expect(h.response).toHaveBeenCalledTimes(1); }); + + describe('it should successfully render a tempalte with dynamic partials', () => { + it('should render a template with dynamic partials', async () => { + let result = ''; + data.template_file = 'pages/page3'; + data.context.template_engine = 'handlebars-v4'; + + h.response = (output) => { + result = output; + return response; + }; + const themeAssembler = { + async getTemplates(templatesPath, processor) { + const templates = await promisify(templateAssembler.assemble)( + path.join(process.cwd(), 'test/_mocks/themes/valid', 'templates'), + templatesPath, + ); + return processor(templates); + }, + getTranslations: async () => { + return {}; + }, + }; + const pencilResponse = new PencilResponse(data, themeAssembler); + await pencilResponse.respond(request, h); + expect(result.content).toEqual(` + +
+