简体   繁体   English

浏览器端Dust.js

[英]Browser side Dust.js

I've been trying to put together a really simple example using Dust.js in the Browser. 我一直在尝试在浏览器中使用Dust.js编写一个非常简单的示例。 Although the docs are very good there doesn't seem to be much information on getting things configured in the for browser. 尽管文档非常好,但是关于在浏览器中进行配置的信息似乎并不多。

So based on the following JSON data taken from the Linked-In Dust tutorial: 因此,基于以下来自Linked-In Dust教程的JSON数据:

https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#wiki-Show_me_some_Dust https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#wiki-Show_me_some_Dust

{
   "title": "Famous People", 
   "names" : [{ "name": "Larry" },{ "name": "Curly" },{ "name": "Moe" }]
}

And this template: 和这个模板:

 {title}
 <ul>
 {#names}
 <li>{name}</li>{~n}
 {/names}
  </ul>

How can this be sent up for rendering client side in the browser? 如何发送此消息以在浏览器中呈现客户端?

I was looking for a browser example of DustJS too since the tutorials don't really mention it. 我也正在寻找DustJS的浏览器示例,因为这些教程并未真正提及它。

Here's a repo I created with an example: https://github.com/ericktai/dust-js-browser 这是我用示例创建的仓库: https : //github.com/ericktai/dust-js-browser

I use duster.js to compile the template. 我使用duster.js编译模板。 The repo's README.md should describe the basics. 仓库的README.md应该描述基础知识。

Hope it helps! 希望能帮助到你!

Erick 埃里克

A little bit down on the page you will find a basic example how to compile a template: 在页面的下方,您会找到一个如何编译模板的基本示例:
Compiling a Dust Template 编译灰尘模板

Basically you just have to call the compile function to register the template. 基本上,您只需要调用compile函数来注册模板。

var compiled = dust.compile("Hello {name}!", "intro");

To use it immediately, execute loadSource to write the compiled template into dust's template library: 要立即使用它,请执行loadSource将编译后的模板写到灰尘的模板库中:

dust.loadSource(compiled);

Then you're able to render the template with your JSON-data: 然后,您可以使用JSON数据render模板:

dust.render("intro", {name: "Fred"}, function(err, out) {
    console.log(out);

    // or maybe with jquery:
    $('#container').html(out);
});

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

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