Skip to main content

Class: RenderContext

A RenderContext manages template scopes, internal registers and access to the bound environment during the rendering of a template.

A new RenderContext is created automatically every time render() is called on a Template, so you probably don't want to instantiate it directly.

Constructors

new RenderContext()

new RenderContext(environment, globals, options): RenderContext

Parameters

environment: Environment

The environment from which this context was created.

globals: ContextScope = {}

Global template variables, passed down from the Environment, Template, Loader and arguments to .render().

options: RenderContextOptions = {}

Extra render context options.

Returns

RenderContext

Defined in

src/context.ts:154

Properties

counters

readonly counters: object = {}

A distinct scope for counters set using the increment and decrement tags.

Index Signature

[index: string]: number

Defined in

src/context.ts:73


disabledTags

readonly disabledTags: Set<string>

A set of tag names that are disallowed in this render context. For example, the include tag is not allowed in templates rendered with the render tag.

Defined in

src/context.ts:120


environment

readonly environment: Environment

The environment from which this context was created.

Defined in

src/context.ts:155


forLoops

readonly forLoops: ForLoopDrop[] = []

A stack of ForLoopDrop objects. Used to populate the parentloop property of a ForLoopDrop.

Defined in

src/context.ts:79


loaderContext

readonly loaderContext: ContextScope

An object containing arbitrary properties passed down from a template loader. The properties of this object are not intended to be accessible by template authors.

Defined in

src/context.ts:145


localsScore

localsScore: number

A non-specific indication of how much the local namespace has been used.

Defined in

src/context.ts:99


loopIterationCarry

readonly loopIterationCarry: number

A loop iteration count carried over from a parent context, if this one has been copied. This helps us adhere to loop iteration limits in templates rendered with the render tag.

Defined in

src/context.ts:106


registers

readonly registers: Map<string | symbol, Map<string | symbol, unknown>>

A register is a Map used by tags and/or filters to store arbitrary values that are not available to template authors. Use getRegister() to obtain a named register.

Defined in

src/context.ts:86


scope

readonly scope: ObjectChain

A chain of scopes. When resolving names, each scope in the chain is searched in order. If a new scope if pushed on to a RenderContext, it is pushed to the front if this chain.

Defined in

src/context.ts:113


template?

optional template: Template

The Template being rendered by this render context.

Defined in

src/context.ts:131


templateName

readonly templateName: string

The name of the template being rendered. Will be <string> for templates parsed using Environment.fromString() without being given a name.

Defined in

src/context.ts:126

Methods

assign()

assign(key, value): void

Assign or re-assign a template local variable, probably from either the assign or capture tags.

Parameters

key: string

The name of the template local variable.

value: unknown

The value of the template local variable.

Returns

void

Defined in

src/context.ts:189


assignScore()

assignScore(key, value): number

Return a size or score for the key and/or value to contribute to the local namespace score. Override this to control how the local namespace score is calculated.

Parameters

key: string

value: unknown

Returns

number

Defined in

src/context.ts:204


copy()

copy(scope, disabledTags, carryLoopIterations, blockScope, template?): RenderContext

Create a new context by copying and extending this one with the given scope.

Parameters

scope: ContextScope

A scope with which to extend the current context.

disabledTags: Iterable<string>

The names of any tags that should be disallowed in the new context.

carryLoopIterations: boolean = false

If true, carry the loop iteration counter.

blockScope: boolean = false

If true, include context locals in the copy.

template?: Template

Optionally attach a new template to the returned copy.

Returns

RenderContext

An extended copy of this context.

Defined in

src/context.ts:375


extend()

extend<T>(scope, callback, template?): Promise<T>

Extend the current scope for the duration of the given callback function.

Type Parameters

T

Parameters

scope: ContextScope

Variables with which to extend the

callback

A function to call with the extended scope.

template?: Template

Returns

Promise<T>

The callback functions return value.

Defined in

src/context.ts:434


extendSync()

extendSync<T>(scope, callback, template?): T

A synchronous version of extend.

Type Parameters

T

Parameters

scope: ContextScope

callback

template?: Template

Returns

T

Defined in

src/context.ts:457


get()

get(name, path?, missing?): Promise<unknown>

Search the current scope for a template variable and, if found, follow the given path. This is a bit like resolving a JSONPath expression.

Parameters

name: string

The name of the template variable to resolve.

path?: ContextPath

An optional array of path elements to follow.

missing?: unknown = Missing

A default value used if the name and path fail to find a value.

Returns

Promise<unknown>

The value at path, starting from the given name, or missing otherwise. If missing is not given, an instance of the Undefined class defined on the attached environment will be used.

Defined in

src/context.ts:239


getRegister()

getRegister(key): Map<string | symbol, unknown>

Fetch a render context register, creating one if it does not exist.

A register is a place for tags and/or filters to store arbitrary data, without leaking said data into the template scope.

Parameters

key: string | symbol

An identifier for the register.

Returns

Map<string | symbol, unknown>

A register.

Defined in

src/context.ts:344


getSync()

getSync(name, path?, missing?): unknown

A synchronous version of RenderContext.get().

Parameters

name: string

path?: ContextPath

missing?: unknown = Missing

Returns

unknown

See

get

Defined in

src/context.ts:267


getTemplate()

getTemplate(name, loaderContext): Promise<Template>

A convenience method for loading a template from the attached environment.

Parameters

name: string

The name or identifier of the template to load.

loaderContext

Additional, arbitrary data that a loader can use to scope or otherwise narrow its search space.

Returns

Promise<Template>

A Template, ready to be rendered.

Throws

NoSuchTemplateError if a template with the given name can not be found.

Defined in

src/context.ts:300


getTemplateSync()

getTemplateSync(name, loaderContext): Template

A synchronous version of RenderContext.getTemplate().

Parameters

name: string

loaderContext = {}

Returns

Template

See

getTemplate

Defined in

src/context.ts:316


makeLoaderContext()

protected makeLoaderContext(loaderContext): ContextScope

Merge a loader context object with the loader context already stored in this render context.

Parameters

loaderContext: ContextScope

Returns

ContextScope

Defined in

src/context.ts:332


raiseForLoopLimit()

raiseForLoopLimit(length): void

Parameters

length: number = 1

Returns

void

Defined in

src/context.ts:353


resolve()

resolve(name): Promise<unknown>

Resolve a template variable by searching the scope chain. Unlike get, resolve performs a single, top level search of the scope chain. It does not expect a dotted or bracketed identifier.

Parameters

name: string

The name of the template variable to resolve.

Returns

Promise<unknown>

The value stored against the given name, or an instance of the Undefined class defined on the attached environment.

Defined in

src/context.ts:216


resolveSync()

resolveSync(name): unknown

Parameters

name: string

Returns

unknown

Defined in

src/context.ts:222