diff --git a/packages/@ember/-internals/glimmer/tests/integration/components/runtime-template-compiler-explicit-test.ts b/packages/@ember/-internals/glimmer/tests/integration/components/runtime-template-compiler-explicit-test.ts index 99dd11c8a8d..7d9c9938af8 100644 --- a/packages/@ember/-internals/glimmer/tests/integration/components/runtime-template-compiler-explicit-test.ts +++ b/packages/@ember/-internals/glimmer/tests/integration/components/runtime-template-compiler-explicit-test.ts @@ -71,6 +71,19 @@ moduleFor( this.assertStableRerender(); } + async '@test Can use `this` from explicit scope'() { + await this.renderComponentModule(() => { + let state = { cls: 'Hello, world!' }; + + return template('
{{this.cls}}
', { + scope: () => ({ this: state }), + }); + }); + + this.assertHTML('
Hello, world!
'); + this.assertStableRerender(); + } + async '@test Can use inline if and unless in strict mode templates'() { await this.renderComponentModule(() => { return template('{{if true "foo" "bar"}}{{unless true "foo" "bar"}}'); diff --git a/packages/@ember/template-compiler/lib/template.ts b/packages/@ember/template-compiler/lib/template.ts index ca83645457e..bd2247eb386 100644 --- a/packages/@ember/template-compiler/lib/template.ts +++ b/packages/@ember/template-compiler/lib/template.ts @@ -270,10 +270,24 @@ function buildEvaluator(options: Partial | undefined) { } return (source: string) => { - const argNames = Object.keys(scope); - const argValues = Object.values(scope); + let hasThis = Object.prototype.hasOwnProperty.call(scope, 'this'); + let thisValue = hasThis ? (scope as { this?: unknown }).this : undefined; - return new Function(...argNames, `return (${source})`)(...argValues); + let argNames: string[] = []; + let argValues: unknown[] = []; + + for (let [name, value] of Object.entries(scope)) { + if (name === 'this') { + continue; + } + + argNames.push(name); + argValues.push(value); + } + + let fn = new Function(...argNames, `return (${source})`); + + return hasThis ? fn.call(thisValue, ...argValues) : fn(...argValues); }; } }