Search code examples
javascripttemplatesyui

How can i precompile a YUI.template.Micro to a TemplateName.js file?


I have been reading up on using YUIs template.micro here: precompiling YUI micro templates but cant see how its possible to return the JS to a templateName . js file. Alternatively, if its not designed to be saved to a templateName . js file - how is it designed to be used? Thanks in advance!


Solution

  • The docs show you how to precompile but it lacks the part where you save it to disk.

    Here's a slightly modified example

    // Load the YUI Template.Micro module.
    var Micro = require('yui/template-micro').Template.Micro;
    
    // Precompile a template string (pass any string you like here).
    var precompiled = Micro.precompile(fs.readFileSync("./template.tmpl"));
    
    // Save the compiled template to disk
    fs.writeFileSync("./template.js", precompiled);
    

    That last step is the important one.