Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/cli-kit/src/public/node/liquid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
20 changes: 18 additions & 2 deletions packages/cli-kit/src/public/node/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,31 @@ 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.
*
* @param templateContent - Template content.
* @param data - Data to feed the template engine.
* @returns Rendered template.
*/
export function renderLiquidTemplate(templateContent: string, data: object): Promise<string> {
const engine = new Liquid()
export async function renderLiquidTemplate(templateContent: string, data: object): Promise<string> {
if (!templateContent.includes('{{') && !templateContent.includes('{%')) {
Comment thread
gonzaloriestra marked this conversation as resolved.
return templateContent
}

const engine = getLiquidEngine()
return engine.render(engine.parse(templateContent), data)
}

Expand Down
Loading