Skip to content

Tutorial04 Using programmatic API

Taka Okunishi edited this page May 2, 2016 · 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"
 */
'use strict'

const 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"
 */
'use strict'

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`
let 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