diff --git a/packages/cli-kit/src/public/node/liquid.test.ts b/packages/cli-kit/src/public/node/liquid.test.ts index eecefcadf32..40e19a2d071 100644 --- a/packages/cli-kit/src/public/node/liquid.test.ts +++ b/packages/cli-kit/src/public/node/liquid.test.ts @@ -14,6 +14,28 @@ describe('create', () => { // Then expect(got).toEqual('test') }) + + test('returns static string as-is when no Liquid syntax is present', async () => { + // Given + const templateContent = 'plain text, no templates' + + // When + const got = await renderLiquidTemplate(templateContent, {variable: 'test'}) + + // Then + expect(got).toEqual(templateContent) + }) + + test('renders strings with only block tags', async () => { + // Given + const templateContent = '{% if true %}yes{% endif %}' + + // When + const got = await renderLiquidTemplate(templateContent, {}) + + // Then + expect(got).toEqual('yes') + }) }) describe('recursiveLiquidTemplateCopy', () => { diff --git a/packages/cli-kit/src/public/node/liquid.ts b/packages/cli-kit/src/public/node/liquid.ts index 47cdb18ff27..c38e7130b8b 100644 --- a/packages/cli-kit/src/public/node/liquid.ts +++ b/packages/cli-kit/src/public/node/liquid.ts @@ -14,6 +14,18 @@ import {joinPath, dirname, relativePath} from './path.js' import {outputContent, outputToken, outputDebug} from './output.js' import {Liquid} from 'liquidjs' +let liquidEngine: Liquid | undefined + +/** + * Returns a shared instance of the Liquid template engine. + * + * @returns Shared Liquid instance. + */ +function getLiquidEngine(): Liquid { + liquidEngine ??= new Liquid() + return liquidEngine +} + /** * Renders a template using the Liquid template engine. * @@ -21,8 +33,12 @@ import {Liquid} from 'liquidjs' * @param data - Data to feed the template engine. * @returns Rendered template. */ -export function renderLiquidTemplate(templateContent: string, data: object): Promise { - const engine = new Liquid() +export async function renderLiquidTemplate(templateContent: string, data: object): Promise { + if (!templateContent.includes('{{') && !templateContent.includes('{%')) { + return templateContent + } + + const engine = getLiquidEngine() return engine.render(engine.parse(templateContent), data) }