简体   繁体   English

Dust.js-在客户端渲染预编译的匿名模板

[英]Dust.js - Render precompiled anonymous template on client side

Is there a way to render a precompiled template that has no name on the client side in DustJs? 有没有办法渲染在DustJs 客户端上没有名称的预编译模板?

Because documentation only shows with a name: 由于文档仅显示名称:

<!-- 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>


EDIT : Ok it's probably too complicated to load a file, and I just noticed that when we compile without specifying name (in order to compile many template simultaneously), the path of the template is set as the default name. 编辑:好的,加载文件可能太复杂了,我只是注意到,当我们在不指定名称的情况下进行编译(以便同时编译多个模板)时,该模板的路径被设置为默认名称。 It is even editable with --pwd flag . 它甚至可以使用--pwd标志进行编辑。
There is therefore always a name so the above function can operate. 因此,总会有一个名称,以便上面的功能可以运行。

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. Dust允许您通过AMD(require.js)兼容性执行此操作。

http://www.dustjs.com/guides/setup/#amd 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. 一旦加载了require.js并设置define.amd.dust define.amd.dust = true ,就可以使用模板的路径调用dust.render ,而Dust会自动为您加载它。

Note that this requires that you compile your templates with the --amd flag. 请注意,这要求您使用--amd标志编译模板。

<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 . Dust存储库中有一个使用AMD加载模板的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM