Skip to main content

Class: Template

A Liquid template that has been parsed and is bound to an environment, ready to be rendered. Rather than constructing a template directly, you should use Template.fromString(), Environment.fromString() or Environment.getTemplate().

Constructors

new Template()

new Template(environment, tree, globals?, templateContext?): Template

Template constructor. Rather than constructing a template directly, you should use Template.fromString(), Environment.fromString() or Environment.getTemplate().

Parameters

environment: Environment

The environment this template is bound to.

tree: Root

The root of the abstract syntax tree representing this template.

globals?: ContextScope

An optional object who's properties will be added to the render context every time the resulting template is rendered.

templateContext?: TemplateContext = {}

Optional meta data. Mostly for managing loading and reloading of templates.

Returns

Template

Defined in

src/template.ts:109

Properties

environment

readonly environment: Environment

Defined in

src/template.ts:41


globals

readonly globals: ContextScope

Defined in

src/template.ts:44


isUpToDate()

readonly isUpToDate: () => Promise<boolean>

Returns

Promise<boolean>

Defined in

src/template.ts:47


isUpToDateSync()

readonly isUpToDateSync: () => boolean

Returns

boolean

Defined in

src/template.ts:48


loaderContext

readonly loaderContext: ContextScope

Defined in

src/template.ts:46


matter

readonly matter: ContextScope

Defined in

src/template.ts:45


name

readonly name: string

Defined in

src/template.ts:43


renderContextClass

protected renderContextClass: typeof RenderContext = RenderContext

Defined in

src/template.ts:49


tree

readonly tree: Root

Defined in

src/template.ts:42

Methods

analyze()

analyze(options): Promise<TemplateAnalysis>

Statically analyze this template and any included/rendered templates. Currently we only analyze references to template variables.

Parameters

options: TemplateAnalysisOptions = {}

Template analysis options.

Returns

Promise<TemplateAnalysis>

Defined in

src/template.ts:276


analyzeSync()

analyzeSync(options): TemplateAnalysis

A synchronous version of analyze

Parameters

options: TemplateAnalysisOptions = {}

Template analysis options.

Returns

TemplateAnalysis

See

analyze

Defined in

src/template.ts:301


handleError()

protected handleError(error, node, blockScope, partial): void

Parameters

error: unknown

node: Node

blockScope: boolean

partial: boolean

Returns

void

Defined in

src/template.ts:165


makeGlobals()

protected makeGlobals(templateGlobals): ContextScope

Combine render context global variables from the bound environment, the "matter" object originating from a template loader (if any) and those passed to render().

Override this to change global template scope priorities.

Parameters

templateGlobals: ContextScope

Returns

ContextScope

Defined in

src/template.ts:266


render()

render(globals): Promise<string>

Render the template.

Parameters

globals: ContextScope = {}

An optional object who's properties will be added to the render context,

Returns

Promise<string>

The rendered template.

Defined in

src/template.ts:131


renderSync()

renderSync(globals): string

A synchronous version of render.

Parameters

globals: ContextScope = {}

Returns

string

See

render

Defined in

src/template.ts:150


renderWithContext()

renderWithContext(context, outputStream, blockScope, partial): Promise<void>

Render a template given an existing render context and output stream. This is used by the built-in include and render tags.

Parameters

context: RenderContext

outputStream: RenderStream

blockScope: boolean = false

partial: boolean = false

Returns

Promise<void>

Defined in

src/template.ts:196


renderWithContextSync()

renderWithContextSync(context, outputStream, blockScope, partial): void

A synchronous version of renderWithContext.

Parameters

context: RenderContext

outputStream: RenderStream

blockScope: boolean = false

partial: boolean = false

Returns

void

See

renderWithContext

Defined in

src/template.ts:223


withGlobals()

withGlobals(globals?): Template

Copy this template with new render context globals.

Parameters

globals?: ContextScope

An object who's properties will be added to the render context every time this template is rendered.

Returns

Template

A copy of this template with new render context globals.

Defined in

src/template.ts:249


from()

static from(source, templateGlobals?, options?): Template

Parse a Liquid template, automatically creating an environment to bind it to.

Alias of fromString

Parameters

source: string

The Liquid template source code.

templateGlobals?: ContextScope

Global render context variables that will included every time this template is rendered.

options?: EnvironmentOptions = {}

Options to set on the implicit environment.

Returns

Template

A new template, bound to an implicit environment.

Defined in

src/template.ts:88


fromString()

static fromString(source, templateGlobals?, options?): Template

Parse a Liquid template, automatically creating an environment to bind it to.

Parameters

source: string

The Liquid template source code.

templateGlobals?: ContextScope

Global render context variables that will included every time this template is rendered.

options?: EnvironmentOptions = {}

Options to set on the implicit environment. globals and loader will be ignored when creating an implicit environment.

Returns

Template

A new template, bound to an implicit environment.

Defined in

src/template.ts:62