我想知道如何在编译的dust 模板中管理帮助程序依赖项,特别是与在客户端上使用相关的帮助程序方法是否与编译的dust 模板捆绑在一起?客户端可能不支持的依赖项怎么办?或者如果该依赖项有多个其他依赖项?
这是我希望能够在客户端上使用的灰尘模板的一个简单示例:
// foo.dust
{@myHelper}
<div>{foo}{bar}</div>
{/myHelper}
// my-helper.js
const isomorphicDep = require('isomorphic-dep');
const nodeDep = require('node-dep');
module.exports = function(dust) {
dust.helpers.myHelper = function(chunk, context, bodies, params) {
// do some stuff using deps
let foo = nodeDep.getFoo();
let bar = isomorphicDep.getBar(params.someInput);
return chunk.render(bodies.block, context.push({ foo, bar });
};
};
谢谢
最佳答案
已编译的模板仅包含有关如何呈现的说明 - 它本身不包含任何客户端代码。
例如,像这样的简单模板:
{@helper}foo{/helper}
编译成这两个指令集:
function body_0(chk, ctx) {
return chk.h("helper", ctx, {
"block": body_1
}, {}, "h");
}
function body_1(chk, ctx) {
return chk.w("foo");
}
渲染模板时,它会要求 Dust 查找名为 helper
的助手并执行它(在 body_0
函数中)。模板中不包含 helper
的代码。
因此,在客户端上,您需要包含一个包含加载正确同构 dep 的帮助程序的文件(例如,node-fetch 与 Whatwg-fetch)。
关于javascript - dust helper 依赖项是否捆绑在已编译的模板中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42428210/