问题
I'm using express-hbs nodejs module and I have an issue using registerAsyncHelper. I need compile a layout in restrict scope because of this I've created a new Handlebars instance and I've created a helper in this instance. But when I compile the layout it returns a weird hash.
My code is something like this:
var hbs = require('express-hbs');
var hbs_temp = hbs.create();
hbs_temp.registerAsyncHelper( 'content', function( text, cb ) {
fs.readFile( 'some-file', { encoding: 'utf8' }, function( err, data ) {
cb( new hbs_temp.SafeString( data ) );
});
});
hbs_temp.compile( '<div> {{content}} </div>' )( );`
Result:
<div> __WEIRD HASH__ </div>
My question is. I've something wrong in my code or this is a "express-hbs" bug? Thank you!
回答1:
The express-hbs module inserts those hashes in place of asynchronously returned values, and replaces them when the async calls have completed. You must use it in the context of Express, as a rendering engine to see this working.
来源:https://stackoverflow.com/questions/23546427/express-hbs-instance-registerasynchelper-weird-hash