Skip to content

Tutorial04 Using programmatic API

okunishinishi edited this page Oct 17, 2015 · 16 revisions

Overview

coz provides programmatic API.

This is usefull when you do not want to install coz globally.

Install coz Locally

$ npm install coz --save-dev

Base Usage.

This is an example to execute rendering via programmatic API.

#!/usr/bin/env node

/**
 * run_rendering.js
 * This is an executable file for "examples/04-from-programmatic-api/run_rendering.js"
 */

var coz = require('coz');

// Render .bud files.
coz.render([
    '**/.*.bud'
], function (err) {
    console.log(err ? err : 'Done!');
});

Execute bud File Itself.

You can mark .bud as an executable file and trigger rendering itself.

#!/usr/bin/env node

/**
 * .exec-me.txt.bud
 * This is a bud file for "examples/04-exec-bud-itself"
 */

module.exports = {
    force: true,
    mode: '444',
    tmpl: 'This file is rendered from: "{{from}}"',
    data: {
        from: require('path').basename(__filename)
    }
};

// If there is no parent, it means that this module is executed directory.
// e.g., `node .exec-me.txt.bud`
var main = module.parent == null;
if (main) {
    // Render this bud file.
    // `__filename` is Node.js reserved word and contains path of this file.
    require('coz').render(__filename);
}

See Also

Tutorials

Links

Clone this wiki locally