自定义车把助手中无法访问上下文数据

时间:2019-10-26 08:43:58

标签: javascript handlebars.js helper koa

我有一个自定义的车把助手功能,以检查两个值是否相等。我在视图中嵌套了帮助程序块标签,但是在第二个嵌套的帮助程序块中无法访问数据

如果我在console.log中记录了辅助函数中的内容,则可以看到数据,但是此后没有传递。

我正在使用带有把手的Koa.js作为模板引擎

我的辅助函数在下面称为“等于”

app.use(views(`${__dirname}/views`, {
    extension: 'handlebars',
    map: { handlebars: 'handlebars' },
    options: {
        helpers: {
            //this is my custom helper function
            equals: (a, b, opts) => {
                if (a === b) return opts.fn(this)
                return opts.inverse(this)
            }
        },
        partials: {
            header: './partials/header',
            footer: './partials/footer',
            banner: './partials/banner',
        },
    }
}))
{{#equals data.user.role 'customer'}}
    <div>
        //this data is not being shown at all
        {{#equals data.job.status "awaiting-quote"}}Awaiting quote{{/equals}}
        {{#equals data.job.status "time-slot-allocated"}}Awaiting appointment{{/equals}}
        {{#equals data.job.status "job-completed"}}Complete{{/equals}}

        //this does work
        //so it appears that data.job.status is undefined in the nested equals
        {{#equals "test" "test"}}this works{{/equals}}

    </div>
{{/equals}}

0 个答案:

没有答案