Repository navigation
feat: add the templateContext feature - #1883
Merged
Merged
Conversation
With disableTemplateContext, renderTemplate, renderTemplateAsync, renderTemplates and renderTemplatesAsync render the templates without the generator as their context: a template reads only its data, and reading a property of `this` throws, naming the template. A context passed in transformOptions is still used. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
disableTemplateContext has three states: - true: no context, reading a property of `this` throws; the default of the next major. - not set (deprecated): the generator, warning once by template and property read from `this`. - false (deprecated): the generator, without a warning. EJS keeps the context with the compiled template, so with its cache a template rendered by several generators has an unpredictable context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
templateContext gives the context of the templates of renderTemplate*: - 'warn' (default): the generator, warning once by template and property read from `this`. - 'throw': reading a property of `this` throws; the default of the next major. - 'none': no context is passed. - 'generator' (deprecated): the generator, without a warning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
templateContext defaults to 'generator', without a warning; 'warn' warns on the use of the generator as the context when set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
renderTemplateandrenderTemplateAsyncrender the templates with the generator as their context (transformOptions.context: this), so a template can read anything from the generator. EJS keeps the context with the compiled template, so with the EJS cache a template rendered by several generators has an unpredictable context.Add the
templateContextfeature, forrenderTemplate,renderTemplateAsync,renderTemplatesandrenderTemplatesAsync:templateContext'generator'(default)'warn'this, through the generator log'throw'thisthrows, naming the template (Template <template path> reads this.<property>: the generator is not the context of its templates, pass it in the template data). The default of the next major.'none'thisundefinedwith'throw', and does not warn with'warn'.'warn', a method read fromthiskeeps the generator asthis(its private fields), and getters run on the generator.contextpassed intransformOptionsis still used._templateData(), from the config) is unchanged.this.fs.copyTpldirectly are not affected.Behavior change
None by default:
'generator'keeps the generator as the context, without a warning.Test plan
eslint,prettier,buildvitest, rendering real templates: the default and'generator'without a warning,'warn'warning once by template and property, a method of the generator called from a template,'none'without a context, the data with'throw'(renderTemplate,renderTemplateAsync,renderTemplates), the error naming the template and the property (sync and async), an explicit context🤖 Generated with Claude Code