Skip to content

Tutorial02 Rendering bud files

Taka Okunishi edited this page May 2, 2016 · 13 revisions

Overview

With coz, you can generate any file with 2 step.

  1. Create a .bud file.
  2. Run coz render command.

A bud contains file meta data like:

  • Which template to use?
  • Where to render it?
  • What permission to give?

, and so on.

More detail about bud is described in the next tutorial: 03 - Mastering coz bud


Basic Usage.

The most simple usage is generate a single file from a single .bud file.

Here is an example:

.who-likes-what.txt.bud (bud file)

/**
 * .who-likes-what.txt.bud
 * This is a bud file for "examples/01-minimum-demo"
 */

// Exports as a Node.js module.
module.exports = {

  // Template string. By default, parsed by Handlebars engine.
  tmpl: '{{#each members}}Hi, my name is {{@key}}. I like {{this}}.\n{{/each}}',

  // Overwrite when already existing.
  force: true,

  // File path to write out.
  path: 'who-likes-what.txt',

  // File permission.
  mode: '444',

  // Data to render.
  data: {
    members: {
      "Mai": "apple",
      "Tom": "Orange",
      "Rita": "Banana"
    }
  }
};

As you see, .bud file is actuary a JavaScript file and could be exported an a Node.js module.

Save this file as .who-likes-what.txt.bud and then, run:

# Render the bud file
$ coz render ".who-likes-what.txt.bud"

This will generate a file named who-likes-what.txt.

who-likes-what.txt (generated file)

Hi, my name is Mai. I like apple.
Hi, my name is Tom. I like Orange.
Hi, my name is Rita. I like Banana.


Separate Template and Data.

For more compile rending, you may want to save template and and to separated files from the .bud file.

tmpl attribute could be a file path.

data attribute may be js/json module, acquired via require.

.what-colors.html.bud (bud file)

/**
 * .what-colors.html.bud
 * This is a bud file for "examples/02-separated-template"
 */

// Exports as a Node.js module.
module.exports = {

  // Template file path. Relative to this bud file.
  tmpl: '.what-colors.html.hbs',

  // Overwrite when already existing.
  force: true,

  // File path to write out.
  path: 'what-colors.html',

  // File permission.
  mode: '444',

  // Data to render.
  data: require('./.what-colors.html.json')
};

.what-colors.html.hbs (template file)

<table>
    <caption>Colors</caption>
    <tbody>
    {{#each colors}}
        <tr><th>{{@key}}</th>{{this}}</tr>
    {{/each}}
    </tbody>
</table>

And run:

# Render the bud file
$ coz render ".what-colors.html.bud"

will generate:

what-colors.html (generated file)

<table>
    <caption>Colors</caption>
    <tbody>
        <tr><th>banana</th>yellow</tr>
        <tr><th>apple</th>red</tr>
        <tr><th>grape</th>purple</tr>
    </tbody>
</table>

See Also

Tutorials

Links

Clone this wiki locally