简体   繁体   English

我该如何安装胡子库?

[英]How should I install mustache library?

There is a file for the mustache folder called mustache.js-master in github. 在github中有一个名为mustache.js-master的mustache文件夹的文件。 It has a number of different files including mustache.min.js, mustache.js, bin folder, hooks folder, spec folder, test, etc. How do I install mustache, do i just download the file and include mustache.js in my code, or is there a way to install the file? 它有许多不同的文件,包括mustache.min.js,mustache.js,bin文件夹,hooks文件夹,spec文件夹,测试等。如何安装小胡子,我只需下载文件并在我的内容中包含mustache.js代码,还是有办法安装文件?

I just included the mustache.js file in my project folder and wrote the tag in my code. 我只是在我的项目文件夹中包含了mustache.js文件,并在我的代码中编写了标记。 Here is some code that I wrote to see if it works, the output from both the console.log and the document.write are empty. 下面是我编写的一些代码,看它是否有效,来自console.log和document.write的输出都是空的。 Is there anything wrong with my code or was I supposed to install mustache in a different way? 我的代码有什么问题,或者我应该以不同的方式安装胡子?

<script type ="text/javascript" src ="jquery.js"></script>
<script type="text/javascript" src = "mustache.js"></script>

<script>

    var template;
    var data;

    template = '<div><h2>{{Title}}</h2><p>{{Course}}</p><p>{{Category}}</p></div>';

    data = '[{"Title":"Algorithms","Course":"CSI241","Category":"science"},{"Title":"Fluid dynamics","Course":"PHY345","Category":"science"}]';

    var html = Mustache.to_html(template,data);

    console.log(html);

    document.write(html);

</script>

Download the mustache.min.js file , since you're not gonna edit that code. 下载mustache.min.js文件 ,因为您不会编辑该代码。

But you have another problem: the to_html expects a template and an object. 但是你有另一个问题: to_html需要一个模板和一个对象。 Your data variable is a string containing some JSON. 您的data变量是包含一些JSON的字符串。 You have to parse it as JSON (if you're getting it as string) or passing it directly as object (if you already know what you have there). 你必须把它解析为JSON(如果你把它作为字符串)或直接作为对象传递(如果你已经知道你有什么)。

I also wrapped your data array into an object like this { data: data } to be able to repeat the snippets. 我还将您的data包装到像{ data: data }这样的对象中,以便能够重复这些片段。

Here is an example: 这是一个例子:

 var template = '{{#data}}<div><h2>{{Title}}</h2><p>{{Course}}</p><p>{{Category}}</p></div>{{/data}}'; var data = JSON.parse('[{"Title":"Algorithms","Course":"CSI241","Category":"science"},{"Title":"Fluid dynamics","Course":"PHY345","Category":"science"}]') var html = Mustache.to_html(template, { data: data }); document.body.innerHTML = html; 
 <script src="https://cdn.rawgit.com/janl/mustache.js/master/mustache.min.js"></script> 

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

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