使用 Pug 过滤器和 jsTransformer-handlebars 模块,我试图将一些把手代码插入到一个将使用本地人的 pug 模板中。然而,由于 Pug 在编译时渲染过滤器,我们不能使用模板 locals。所以我想知道让 jsTransformer 在浏览器上可用是否会解决这个问题,如果是,如何打包 jsTransformer-handlebars 模块并使其在浏览器上可用。这是问题的概要(有点长,我很抱歉。我想提供大量细节以确保问题清晰):
我有以下通常用于车把的辅助函数):
//helpers.js
module.exports = {
truncate: (str, len) => {
//code
return str;
},
stripTags: input => input.replace(/<(?:.|\n)*?>/gm, ''),
formatDate: (date, format) => moment(date).format(format),
select: function (selected, options) {
return options.fn(this).replace(new RegExp(` value="${selected}"`), '$&selected="selected"').replace(new RegExp(`>${selected}</option>`), 'selected="selected"$&');
},
};
Run Code Online (Sandbox Code Playgroud)
helpers.js 在 app.js 中被导入并且函数被设置为应用程序变量。除了 select() 之外,它们都可以在 pug 中使用。
//sets application locals so I can call these functions from a pug template
app.locals = {
truncate,
stripTags,
formatDate,
select, …Run Code Online (Sandbox Code Playgroud)