Reputation: 189
I have an sample template in Dust js and compiled that source using dust-down.
When i added (using script tag) that template js into my code, i can able to see the compiled object in console. But how can i use that object?
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
How can i pass the object to that compiled object ? In other means instead of dust.render("dusttest",obj,function(){}), how can i directly use that compiled object?
Upvotes: 1
Views: 1965
Reputation: 2632
I've not used dust-down, but I did review the latest source code for it.
These are the lines that do the actual compilation:
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);
Since it's using dust.compile(), then the usage should be the same as in the docs. Since the compiled result is javascript, you should reference it like a normal js script.
<script type='text/javascript' src='/mycompilecode/mytemplate-1.0.js'></script>
Per the docs, when the code loads, it will contain and execute the dust.register() method and thereafter be available for use.
If you'll notice above at how dust-down uses compile, it uses the base filename as the key, and that is what you want to pass to dust.loadSource().
... so, let's put that all together...
The following code is how it would be used (assuming dust is already loaded):
<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>
Upvotes: 2