English | 简体中文

For Node.js

nodetpl addition to general web project can also be used in node.js.

Installation nodetpl

npm install nodetpl --save

The basic syntax

var fs = require('fs');
var nodetpl = require('nodetpl');

var filepath = '/usr/local/nodetpl/test.tpl';
var data = {
  name: 'zhangsan',
  age: 18
};

fs.readFile(filepath, 'utf-8', function(err, content) {
  if (err) {
    throw err;
  } else {
    nodetpl.render(content, data, function(d){
      console.log(d);
    });
  }
});

For Express.js

'use strict';

var path = require('path');
var express = require('express');
var app = express();

app.set('view engine', 'tpl');
app.set('views', path.join(__dirname, 'views'));
app.engine('tpl', require('nodetpl/express'));

app.get('/', function(req, res) {
  res.render('index', function(err, html) {
    if (err) {
      res.send(err.toString());
    } else {
      res.send(html);
    }
  });
});

app.listen(3000, function() {
  console.log('listening on port 3000');
});

Communication