Skip to content
Draft
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: 20 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,33 @@ 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> {
// Optimization: If the content doesn't contain any Liquid tags, we can return it as is.
// This avoids the overhead of parsing and rendering the template.
if (!templateContent.includes('{{') && !templateContent.includes('{%')) {
return templateContent
}

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

Expand Down
Loading