Skip to content

Commit e739bcc

Browse files
committed
fix(inertia-sails): ensure locals are accessible via locals.xxx in EJS templates
Sails's default EJS renderer creates an `options.locals` object for internal helpers (blocks, layout, partial). EJS wraps templates in `with(data) { ... }`, which causes `locals` inside the template to resolve to `data.locals` (the Sails-created object) rather than the function parameter containing the actual view data. This made `<%= locals.title %>` always return undefined. Fix by pre-populating `data.locals` with user-provided locals so they survive the `with` scoping. Closes #188
1 parent 0065895 commit e739bcc

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

packages/inertia-sails/lib/render.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ module.exports = async function render(req, res, data) {
3232
return res.json(page)
3333
} else {
3434
// Implements full page reload
35-
return res.view(rootView, { page, ...allLocals })
35+
//
36+
// We pass locals both as top-level properties AND nested under a `locals`
37+
// key. This is necessary because Sails's default EJS renderer creates an
38+
// `options.locals` object (for blocks, layout, partial helpers). EJS wraps
39+
// templates in `with(data) { ... }`, so inside the template `locals`
40+
// resolves to `data.locals` (the nested object) rather than the `locals`
41+
// function parameter. By pre-populating `data.locals` with our values,
42+
// `<%= locals.title %>` in the EJS template correctly resolves to the
43+
// dynamic value instead of undefined.
44+
return res.view(rootView, { page, ...allLocals, locals: { ...allLocals } })
3645
}
3746
}

0 commit comments

Comments
 (0)