diff --git a/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.js b/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.js index 4040c26380e..df1164c3e3a 100644 --- a/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.js +++ b/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.js @@ -44,8 +44,8 @@ export function registerRawHelpers(hbs, handlebarsClass, owner) { } let list = get(this, contextName); let output = []; + let innerContext = { ...options.contexts[0] }; for (let i = 0; i < list.length; i++) { - let innerContext = {}; innerContext[localName] = list[i]; output.push(options.fn(innerContext)); } diff --git a/app/assets/javascripts/discourse/tests/integration/helpers/raw-test.gjs b/app/assets/javascripts/discourse/tests/integration/helpers/raw-test.gjs index f119d9de3b4..6f69d142dbb 100644 --- a/app/assets/javascripts/discourse/tests/integration/helpers/raw-test.gjs +++ b/app/assets/javascripts/discourse/tests/integration/helpers/raw-test.gjs @@ -7,6 +7,7 @@ import RenderGlimmerContainer from "discourse/components/render-glimmer-containe import raw from "discourse/helpers/raw"; import rawRenderGlimmer from "discourse/lib/raw-render-glimmer"; import { setupRenderingTest } from "discourse/tests/helpers/component-test"; +import { compile } from "discourse-common/lib/raw-handlebars"; import { addRawTemplate, removeRawTemplate, @@ -104,4 +105,20 @@ module("Integration | Helper | raw", function (hooks) { assert.dom("span.bar").hasText(/^baz$/); }); + + test("#each helper preserves the outer context", async function (assert) { + const template = ` + {{#each items as |item|}} + {{string}} {{item}} + {{/each}} + `; + addRawTemplate("raw-test", compile(template)); + + const items = [1, 2]; + await render(); + + assert.dom("span").hasText("foo 1 foo 2"); + }); });