1

有没有办法在 DustJs的客户端呈现一个没有名称的预编译模板?

因为文档仅显示名称:

<!-- precompiled templates -->
<script type="text/javascript" src="/lib/templates.js"></script>
<script type="text/javascript">
// The templates are already registered, so we are ready to render!
dust.render('hello', { world: "Saturn" }, function(err, out) {
  document.getElementById('output').textContent = out;
})
</script>


编辑:好的,加载文件可能太复杂了,我刚刚注意到,当我们在不指定名称的情况下进行编译时(为了同时编译多个模板),模板的路径被设置为默认名称。它甚至可以使用--pwd flag进行编辑。
因此,总是有一个名称,因此上述功能可以运行。

4

1 回答 1

2

It sounds like you would like to load templates by their path after they have been precompiled. Dust allows you to do this via AMD (require.js) compatibility.

http://www.dustjs.com/guides/setup/#amd

Once you've loaded require.js and set define.amd.dust = true, you can call dust.render with the path to a template and Dust will automatically load it for you.

Note that this requires that you compile your templates with the --amd flag.

<script src="r.js"></script>
<script type="text/javascript">
    define.amd.dust = true;
    require(["lib/dust-full"], function(dust) {
        dust.render('path/to/your/template', function(err, out) { ... });
    });
</script>

The Dust repository has an example of using AMD to load templates.

于 2015-07-24T18:13:24.307 回答