0.0.1 • Published 9 years ago

tyk-stb v0.0.1

Weekly downloads
5
License
-
Repository
-
Last release
9 years ago

stb

stb builds a template based on a definition. Useful for compiling html.

Usage

./app.js

/* jshint node: true */
'use strict';

var build = require('../../'),
    fs = require('fs');

var findContentById = function(path) {
  return function(id, callback) {
    fs.readFile(path + id, function(err, result) {
      if(err) { return callback(err); }
      try { callback(null, JSON.parse(result)); }
      catch(err) { return callback(err); }      
    });
  };
};

var definition = {
      template: fs.readFileSync(__dirname + '/definition/template.txt', 'utf-8'),
      config: require('./definition/config.json')
    };

var run = build(findContentById(__dirname + '/content/'));

run(definition, function(err, template) {
  console.log(template);
});

./definition/template.txt

<div>
  <h1><%= content.title %></h1>
  <%= content.content %>
  <h2>Contact</h2>
  <ul>
    <li>Phone: <%= contact.phone %></li>
    <li>Email: <%= contact.email %></li>
  </ul>
</div>

./definition/config.json

{
  "content": "home.json",
  "contact": "contact.json"
}

./content/contact.json

{
  "phone": "0400123456",
  "email": "info@acme.com"
}

./content/home.json

{
  "title": "Welcome",
  "content": "<p>Bacon ipsum dolor amet porchetta ham hock pork andouille kevin pig cow turducken picanha ball tip biltong. Pancetta porchetta salami, picanha kevin capicola ball tip. Hamburger tail rump, leberkas tenderloin jerky boudin. Ribeye cupim filet mignon fatback beef beef ribs.</p>"
}

Output

<div>
  <h1>Welcome</h1>
  <p>Bacon ipsum dolor amet porchetta ham hock pork andouille kevin pig cow turducken picanha ball tip biltong. Pancetta porchetta salami, picanha kevin capicola ball tip. Hamburger tail rump, leberkas tenderloin jerky boudin. Ribeye cupimfilet mignon fatback beef beef ribs.</p>
  <h2>Contact</h2>
  <ul>
    <li>Phone: 0400123456</li>
    <li>Email: info@acme.com</li>
  </ul>
</div>