Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用dust js的编译源代码_Javascript_Dust.js - Fatal编程技术网

Javascript 如何使用dust js的编译源代码

Javascript 如何使用dust js的编译源代码,javascript,dust.js,Javascript,Dust.js,我在Dust js中有一个示例模板,并使用Dust down编译了该源代码 当我在代码中添加(使用脚本标记)该模板js时,我可以在控制台中看到编译后的对象。但是我怎样才能使用那个物体呢 Example : Created a compile file with the name dusttest.js and included it in my HTML page. In firebug console, i am able to view the object like dust.cache

我在Dust js中有一个示例模板,并使用Dust down编译了该源代码

当我在代码中添加(使用脚本标记)该模板js时,我可以在控制台中看到编译后的对象。但是我怎样才能使用那个物体呢

Example :

Created a compile file with the name dusttest.js and included it in my HTML page.
In firebug console, i am able to view the object like dust.cache.dusttest
如何将该对象传递给该已编译对象?
用其他方法代替dust.render(“dusttest”,obj,function(){}),我如何直接使用编译后的对象呢?

我没有使用dust-down,但我查看了它的最新源代码。
以下是进行实际编译的行:

var filename = filepath.split("/").reverse()[0].replace(".html", "").replace(".dust", "");
file_to_create = file_to_create + "\\" + filename + "_"  + version + ".js";

//compile to dust
// notice the second parameter to compile() is the BASE of the filename!
var compiled = dust.compile(data, filename);
因为它使用的是dust.compile(),所以用法应该与文档中的用法相同。因为编译的结果是javascript,所以应该像普通js脚本一样引用它

<script type='text/javascript' src='/mycompilecode/mytemplate-1.0.js'></script>

根据,当代码加载时,它将包含并执行dust.register()方法,然后可供使用

如果您注意到上面dust-down如何使用compile,它使用基本文件名作为键,这就是您要传递给dust.loadSource()的内容

。。。所以,让我们把这些放在一起

  • 以文件“intro.html”作为模板开始
  • 使用dust-down编译它。我假设结果是这样的(如果没有在命令行中指定dust-down,则0\u 0\u 1是默认版本): /templates/compiled/intro_0_0_1.js
  • 下面的代码是如何使用它(假设已装入灰尘):

    
    var myCompiledTemplateKey='intro';//注意!没有版本或.js扩展名
    dust.loadSource(myCompiledTemplateKey);
    render(myCompiledTemplateKey,{name:“Fred”},函数(err,out){
    控制台。注销;
    });
    
    <script type='text/javascript' src='/templates/compiled/intro_0_0_1.js'></script>
    <script type='text/javascript'>
        var myCompiledTemplateKey = 'intro'; // notice! no version or .js extension
        dust.loadSource(myCompiledTemplateKey );
        dust.render(myCompiledTemplateKey , {name: "Fred"}, function(err, out) {
            console.log(out);
        });
    </script>