本文介绍了express-hbs实例注册AsyncHelper怪异哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用express-hbs nodejs模块,并使用 registerAsyncHelper 来解决问题。我需要在限制范围内编译一个布局,因为我已经创建了一个新的Handlebars实例,并且在此实例中创建了一个帮助器。但是,当我编译布局时,它会返回一个奇怪的散列。
我的代码是这样的:

  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>')();`

结果:

< div> __WEIRD HASH__< / div>



我的问题是。我的代码有问题,或者这是一个express-hbs错误?
谢谢!

解决方案

express-hbs模块插入这些散列来代替异步返回值,并替换它们当异步呼叫完成时。您必须在Express环境中使用它,作为渲染引擎才能看到此功能。


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!

解决方案

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.

这篇关于express-hbs实例注册AsyncHelper怪异哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:34