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
Defined in
Properties
counters
readonly
counters:object
={}
A distinct scope for counters set using the increment
and
decrement
tags.
Index Signature
[index
: string
]: number
Defined in
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
environment
readonly
environment:Environment
The environment from which this context was created.
Defined in
forLoops
readonly
forLoops:ForLoopDrop
[] =[]
A stack of ForLoopDrop
objects. Used to populate the parentloop
property of a ForLoopDrop
.
Defined in
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
localsScore
localsScore:
number
A non-specific indication of how much the local namespace has been used.
Defined in
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
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
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
template?
optional
template:Template
The Template
being rendered by this render context.
Defined in
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
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
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
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
An extended copy of this context.
Defined in
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
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
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
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
getSync()
getSync(
name
,path
?,missing
?):unknown
A synchronous version of RenderContext.get()
.
Parameters
• name: string
• path?: ContextPath
• missing?: unknown
= Missing
Returns
unknown
See
Defined in
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
getTemplateSync()
getTemplateSync(
name
,loaderContext
):Template
A synchronous version of RenderContext.getTemplate()
.
Parameters
• name: string
• loaderContext = {}
Returns
See
Defined in
makeLoaderContext()
protected
makeLoaderContext(loaderContext
):ContextScope
Merge a loader context object with the loader context already stored in this render context.
Parameters
• loaderContext: ContextScope
Returns
Defined in
raiseForLoopLimit()
raiseForLoopLimit(
length
):void
Parameters
• length: number
= 1
Returns
void
Defined in
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
resolveSync()
resolveSync(
name
):unknown
Parameters
• name: string
Returns
unknown