Express-hbs: Asynchronous helper with options / attrs

I work with express-hbs and Async helpers, but I need send options to the assistant, but asynchronous helpers do not support this function (or I don’t know how). As you can see (code below), I am trying to load the helper / ADS component, but I need to send additional information / attrs for rendering based on orientation.

Javascript

hbs.registerAsyncHelper( 'ads', function(filename, cb, options) {

       // options: is undefined

      // LOAD ADS FROM REMOVE SERVER.
       cb( new hbs.SafeString( ' == ADS  == ' ) );
});

HTML

{{{ads'page-x', 'vertical', '256x56' }}

Can anyone help me in this situation? Thank!

+4
source share
1 answer

Short answer, you can currently (v0.7.10) supply only one parameter in express-hbs.

, , JSON , JSON.parse(), . .

:.

hbs.registerAsyncHelper( 'ads', function(arg, cb) {
    var options  = JSON.parse(arg);
    console.log(options);
    // LOAD ADS FROM REMOVE SERVER.
    cb( new hbs.SafeString( ' == ADS  == ' ) );
});

{{{ads "[\"page-x\",\"vertical\",\"256x56\"]" }}}

-hbs:

;

express-hbs registerAsyncHelper:

ExpressHbs.prototype.registerAsyncHelper = function(name, fn) {
    this.handlebars.registerHelper(name, function(context) {
        return async.resolve(fn.bind(this), context);
    });
};

() handlebars.registerHelper, . , :

ExpressHbs.prototype.registerAsyncHelper = function(name, fn) {
    this.handlebars.registerHelper(name, function(contextArgs) {
        return async.resolve(fn.bind(this), Array.prototype.slice.call(contextArgs));
    });
};

. , .

, , - . , ( handlebars-hbs ):

{{{ads 'page-x' 'vertical' '256x56'}}}

0

Source: https://habr.com/ru/post/1541651/


All Articles