node.js - How to register a Handlebars partial outside partials folder on Sails.js? -
i have following folder structure on sails.js application:
assets/ mymodule/ views/ partials/
mymodule
backbone component (although client not full backbone application)
there's given handlebar partial x.handlebars
need rendered both via server , client sides.
my struggle starts when views/
not accessible on client side there's no way load on backbone module.
then have tried move assets/mymodule/templates/
accessible main template views/layout.handlebars
can't load partial if try like:
{{> ../assets/mymodule/templates/x}}
which doesn't work (i assume handlebars use views/ root level layouts).
there's 2 possible solutions i'm seeing in situation:
duplicate these layouts , use 1 on each location (not ideal, works)
place on
assets/mymodules/templates/x
, register handlebars partial on sails let available server-side rendering.
how second solution? possible register global partial same way can helpers function under config/views.js
?
my goal use handlebars.registerpartial
function on config level, outside controller partial reused other templates well.
duplicating code not necessary. there many different ways can implement , depends on optimization profile looking for.
a simple solution read hbs file in api/controller node's fs, compile handlebars , pass template this:
var rawtemplate = fs.readfilesync('assets/hbs/client-side-template.hbs', "utf8"); var processedtemplate = handlebars.compile(rawtemplate); return res.render('whiteboard/dualloadview', { mypartialasvariable: processedtemplate({}) });
you'll have have figure out how handle context , variables used partials. can improve on code snippet looking keeping file read in memory, , looking how register partials handlebars instead of using variable.
Comments
Post a Comment