Passing objects to be used by partials inside a layout
-
21-12-2019 - |
Question
I have a page layout with a sidebar and a main content. Both are dynamic according to the user profile.
<html>
<body>
<%- partial('partials/sidebar')%>
<html with <% ejs code %> for body>
</body>
</html>
Sails view should be:
res.view( {mydata} );
How can I pass data to the partials so it can be dynamically rendered instead of static?
Example:
controller:
res.view( {mainContent:{mainContentJson}, sidebar:{sidebarJson} );
layout.ejs:
<html>
<body>
<%- partial('partials/sidebar', sidebar)%>
<html with ejs code for body>
</body>
</html>
Solution
Yes. Partials accept arguments to be passed, and they are rendered before being inserted into the calling layout.
controller:
res.view( {
layout:<yourCustomLayoutIfNeeded>,
mainContent: <mainContentObject>,
partialContent: <partialContentObject>
});
yourCustomLayoutIfNeeded:
<%- partial(<pathToPartial>, partialContent) %>
<mainContent>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow